6๏ธโƒฃ C# Arrays & Strings
Estimated reading: 3 minutes 25 views

๐Ÿ“š C# Arrays โ€“ Master Fixed-Size Collections in C#


๐Ÿงฒ Introduction โ€“ Why Learn Arrays in C#?

Arrays are one of the fundamental building blocks of any programming language. In C#, arrays allow you to store multiple elements of the same data type using a single variable name and access them by index. They’re ideal when working with static datasets like top scores, temperature logs, or fixed-form inputs.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • What arrays are and how they work
  • Array declaration, initialization, and traversal
  • Multi-dimensional and jagged arrays
  • Best practices, examples, and real-world use cases

๐Ÿ” Core Concept โ€“ What Is an Array in C#?

An array is a fixed-length collection that holds elements of the same type. The size is defined at creation and cannot change afterward.

๐Ÿ”ฃ Syntax:

data_type[] arrayName = new data_type[size];

๐Ÿ“˜ Example:

int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

๐Ÿ’ป Code Example โ€“ Array Declaration and Access

string[] fruits = { "Apple", "Banana", "Cherry" };

for (int i = 0; i < fruits.Length; i++)
{
    Console.WriteLine(fruits[i]);
}

๐Ÿ“ฅ Output:

Apple  
Banana  
Cherry

๐Ÿงต Explanation:

  • Array is initialized with 3 strings.
  • .Length property gets total number of elements.
  • Loop prints each item by index.

๐Ÿ” Traversing Arrays โ€“ Foreach Loop

int[] scores = { 90, 85, 78 };

foreach (int score in scores)
{
    Console.WriteLine(score);
}

๐Ÿ“˜ Use Case: Read-only access to elements with simpler syntax.


๐Ÿง  Multi-Dimensional Arrays

int[,] matrix = new int[2, 3] { {1, 2, 3}, {4, 5, 6} };

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        Console.Write($"{matrix[i, j]} ");
    }
    Console.WriteLine();
}

๐Ÿ“ฅ Output:

1 2 3  
4 5 6

๐Ÿ“˜ Use Case: Matrices, tables, spreadsheets, game boards


๐Ÿงฉ Jagged Arrays (Array of Arrays)

int[][] jagged = new int[2][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };

foreach (int[] row in jagged)
{
    foreach (int val in row)
    {
        Console.Write(val + " ");
    }
    Console.WriteLine();
}

๐Ÿ“ฅ Output:

1 2  
3 4 5

๐Ÿ“˜ Use Case: Irregular tables, variable-length rows


๐Ÿ’ก Best Practices & Tips

๐Ÿ’ก Tip: Always check .Length to avoid IndexOutOfRangeException.

โš ๏ธ Pitfall: Arrays are zero-indexed; the first element is at index 0.

๐Ÿ“˜ Best Practice: Use foreach when you donโ€™t need to modify array contents by index.


๐Ÿ“Š Comparison โ€“ Arrays vs Lists

FeatureArray ([])List (List<T>)
SizeFixedDynamic
PerformanceFaster for fixed-size dataMore flexible
SyntaxSimpleRequires System.Collections.Generic
Use CaseStatic datasetsDynamic collections

๐Ÿ› ๏ธ Real-World Use Cases

  • ๐ŸŽฎ Storing game high scores
  • ๐Ÿงฎ Performing matrix operations
  • ๐Ÿ“‹ Handling fixed-size form inputs
  • ๐Ÿ–ฅ๏ธ Mapping 2D grid-based layouts
  • ๐Ÿ”ข Collecting sensor data with a known sample size

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

๐Ÿงต Key Takeaways:

  • Arrays hold fixed-size, same-type data.
  • Use indexers (array[i]) and .Length for access.
  • C# supports 1D, multi-dimensional, and jagged arrays.

โš™๏ธ Real-world relevance: Arrays are core to data processing, grid-based UIs, game development, and static data storage.


โ“ FAQ Section

โ“ Are C# arrays zero-based?
โœ… Yes, the index starts at 0. array[0] accesses the first element.


โ“ Can I resize an array in C#?
โœ… No. Arrays are fixed-size. Use List<T> for dynamic resizing.


โ“ What is the difference between int[] and int[,]?
โœ… int[] is a single-dimensional array. int[,] is a multi-dimensional (rectangular) array.


โ“ How do I find the length of an array?
โœ… Use .Length:

int length = array.Length;

โ“ Can I return an array from a method?
โœ… Yes:

int[] GetNumbers() => new int[] {1, 2, 3};

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“š C# Arrays

Or Copy Link

CONTENTS
Scroll to Top