Introduction
In C#, an array is a collection of elements of the same type that are stored in contiguous memory locations and can be accessed using an index. Arrays provide an efficient way of storing and accessing a fixed number of elements.
To declare an array in C#, you specify the data type of the elements in the array, followed by the name of the array, and then the size of the array in square brackets. For example, the following code declares an array of integers with ten elements.
int[] numbers = new int[10];
The syntax to declare an array is the data type of its elements, followed by the array name. On the right side, use the new keyword and the array size.
For example
int[] intArray = new int[5];
The above code snippet creates an array called "intArray" to hold five integers. However, the elements of the Array are not yet initialized, and their values are undefined.
Arrays can also be initialized when they are declared by providing a list of comma-separated values enclosed in curly braces {}
int[] intArray = new int[] {1, 2, 3, 4, 5};
or simply
int[] intArray = {1, 2, 3, 4, 5};
This creates an array called "intArray" with five elements and assigns the values 1, 2, 3, 4, and 5 to the elements of the Array.
The following code snippet declares an array that can store 100 items from index 0 to 99.
int[] intArray;
intArray = new int[100];
Create an array
There are multiple ways to create an array in C#. Here are a few examples.
1. Using the new keyword.
int[] myArray = new int[5];
This creates an array called "myArray" that can hold five integers. Unfortunately, the elements of the Array are not yet initialized, and their values are undefined.
2. Using the new keyword with an array initializer
int[] myArray = new int[] {1, 2, 3, 4, 5};
or simply
int[] myArray = {1, 2, 3, 4, 5};
This creates an array called "myArray" with five elements and assigns the values 1, 2, 3, 4, and 5 to the elements of the Array.
3. Using the var keyword
var myArray = new int[] {1, 2, 3, 4, 5};
This creates an array; the array type is inferred from the initializer, and the Array's name is myArray.
4. Using the stackable keyword
int* myArray = stackalloc int[5];
The stackable keyword allocates memory on the stack rather than the heap. This creates an unmanaged array of integers called "myArray" that can hold five integers. The elements of the Array are not yet initialized, and their values are undefined.
It is worth noting that stack-allocated arrays should be used with care, as they can cause stack overflow if the array size is too large or if they are used in a recursive function.
Initialize Array Initialization
Once an array is created using one of the above methods, the step is initializing an array. The initialization process of an array includes adding actual data to the Array.
The following code snippet creates an array of 3 items, and the values of these items are added when the Array is initialized.
int[] staticIntArray = { 1, 3, 5 };
Or you could initialize the same Array in this way.
// Initialize a fixed array
int[] staticIntArray = new int[3] {1, 3, 5};
Alternatively, we can add array items individually, as listed in the following code snippet.
// Initialize a fixed array one item at a time
int[] staticIntArray = new int[3];
staticIntArray[0] = 1;
staticIntArray[1] = 3;
staticIntArray[2] = 5;
Note. C# arrays are 0-indexed, meaning that the first element has an index of 0, the second element has an index of 1, and so on.
The following code snippet declares a dynamic array with string values.
// Initialize a dynamic array items during declaration
string[] strArray = new string[] { "Mahesh Chand", "Mike Gold", "Raj Beniwal", "Praveen Kumar", "Dinesh Beniwal" };
Access elements in an Array
We can access an array item by passing the item index in the Array. For example, the following code snippet creates an array of three items and displays those items on the console.
// Initialize a fixed array one item at a time
int[] staticIntArray = new int[3];
staticIntArray[0] = 1;
staticIntArray[1] = 3;
staticIntArray[2] = 5;
// Read array items one by one
Console.WriteLine(staticIntArray[0]);
Console.WriteLine(staticIntArray[1]);
Console.WriteLine(staticIntArray[2]);
This method is proper when you know what item to access from an array. You will get an error if you try to pass an item index more significant than the elements in an array.
Loop through an Array
For each control statement (loop) is used to iterate through the elements of an array. For example, the following code uses a for each loop to read all items of an array of strings.
// Initialize a dynamic array items during declaration
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"Dinesh Beniwal"
};
// Read array items using foreach loop
foreach(string str in strArray) {
Console.WriteLine(str);
}
This approach is used when you do not know the exact index of an item in an array and need to loop through all the items.
Types of Arrays
There are four types of arrays in C#.
- Single-dimensional arrays
- Multi-dimensional arrays or rectangular arrays
- Jagged arrays
- Mixed arrays.
Single Dimension Array
Single-dimensional arrays are the simplest form of arrays. These arrays are used to store the number of items of a predefined type. All items in a single-dimension array are stored contiguously, starting from 0 to the size of the Array -1.
The following code declares an integer array that can store three items. As you can see from the code, first, I say the Array using [] bracket, and after that, I instantiate the collection by calling the new operator.
int[] intArray;
intArray = new int[3];
Array declarations in C# are pretty simple. First, you put array items in curly braces ({}). Then, suppose an array is not initialized. In that case, its items are automatically initialized to the initial default value for the array type if the Array is not initialized when it is declared.
The following code declares and initializes an array of three items of integer type.
int[] staticIntArray = new int[3] {1, 3, 5};
The following code declares and initializes an array of 5 string items.
string[] strArray = new string[5] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };
You can even directly assign these values without using the new operator.
string[] strArray = { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };
You can initialize a dynamic length array as follows.
string[] strArray = new string[] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };
Multi-Dimensional Array
A multi-dimensional array, also known as a rectangular array, has more than one dimension. The form of a multi-dimensional array is a matrix.
Declaring a multi-dimensional array
A multi-dimension array is declared as follows.
string[,] mutliDimStringArray;
A multi-dimensional array can be fixed-sized or dynamic-sized.
Initializing multi-dimensional arrays
The following code snippet is an example of fixed-sized multi-dimensional arrays that define two multi-dimension arrays with a matrix of 3x2 and 2x2. The first Array can store six items, and the second Array can store six items. Both of these arrays are initialized during the declaration.
int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string[,] names = new string[2, 2] { { "Rosy", "Amy" }, { "Peter", "Albert" } };
Now let's see examples of multi-dimensional dynamic arrays where you are unsure of the number of items of the Array. For example, the following code snippet creates two multi-dimensional arrays with no limit.
int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string[,] names = new string[,] { { "Rosy", "Amy" }, { "Peter", "Albert" } };
You can also omit the new operator as we did in single-dimension arrays. You can assign these values directly without using the new operator. For example.
int[,] numbers = {
{ 1, 2 },
{ 3, 4 },
{ 5, 6 }
};
string[,] names = {
{ "Rosy", "Amy" },
{ "Peter", "Albert" }
};
We can also initialize the array items one item at a time. The following code snippet is an example of initializing array items one at a time.
int[, ] numbers = new int[3, 2];
numbers[0, 0] = 1;
numbers[1, 0] = 2;
numbers[2, 0] = 3;
numbers[0, 1] = 4;
numbers[1, 1] = 5;
numbers[2, 1] = 6;
Accessing multi-dimensional arrays
Multi-dimensional array items are represented in a matrix format; we need to specify the matrix dimension to access its items. For example, item(1,2) means an array item in the matrix in the second row and third column.
The following code snippet shows how to access the number array defined in the above code.
Console.WriteLine(numbers[0, 0]);
Console.WriteLine(numbers[0, 1]);
Console.WriteLine(numbers[1, 0]);
Console.WriteLine(numbers[1, 1]);
Console.WriteLine(numbers[2, 0]);
Console.WriteLine(numbers[2, 2]);
Jagged Arrays
Jagged arrays are arrays of arrays. The elements of a jagged array are other arrays.
Declaring Jagged Arrays
The declaration of a jagged array involves two brackets. For example, the following code snippet declares a jagged array that has three items of an array.
int[][] intJaggedArray = new int[3][];
The following code snippet declares a jagged array with two Array items.
string[][] stringJaggedArray = new string[2][];
Initializing Jagged Arrays
Before a jagged array can be used, its items must be initialized. For example, the following code snippet initializes a jagged array; the first item with an array of integers with two integers, the second item with an array of integers with four integers, and the third item with an array of integers with hassix6 integers.
// Initializing jagged arrays
intJaggedArray[0] = new int[2];
intJaggedArray[1] = new int[4];
intJaggedArray[2] = new int[6];
We can also initialize a jagged array's items by providing the values of the Array's items. For example, the following code snippet initializes an array's items directly during the declaration.
// Initializing jagged arrays
intJaggedArray[0] = new int[2] {
2,
12
};
intJaggedArray[1] = new int[4] {
4,
14,
24,
34
};
intJaggedArray[2] = new int[6] {
6,
16,
26,
36,
46,
56
};
Accessing Jagged Arrays
We can access a jagged array of items individually in the following way:
Console.Write(intJaggedArray3[0][0]);
Console.WriteLine(intJaggedArray3[2][5]);
The Length property of an array helps a lot; it gives us the number of items in an array. We can also loop through all of the items of a jagged array. The following code snippet loops through all of the items of a jagged array and displays them on the screen.
// Loop through all itesm of a jagged array
for (int i = 0; i < intJaggedArray3.Length; i++) {
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < intJaggedArray3[i].Length; j++) {
System.Console.Write("{0}{1}", intJaggedArray3[i][j], j == (intJaggedArray3[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
Mixed Arrays
Mixed arrays are a combination of multi-dimensional arrays and jagged arrays. The mixed arrays type is removed from .NET 4.0. I have not seen any use of mixed arrays. You can do anything you want with the help of multi-dimensional and jagged arrays.
A Simple Example
Here is a complete example listed in Listing 1 that demonstrates how to declare all kinds of arrays, initialize them, and access them.
To test this code, create a console application using Visual Studio 2010 or Visual C# Express and copy and paste this code.
Console.WriteLine("Single Dimension Array Sample");
// Single dim array
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"Dinesh Beniwal"
};
// Read array items using foreach loop
foreach(string str in strArray) {
Console.WriteLine(str);
}
Console.WriteLine("-----------------------------");
Console.WriteLine("Multi-Dimension Array Sample");
string[,] string2DArray = new string[2, 2] {
{
"Rosy",
"Amy"
}, {
"Peter",
"Albert"
}
};
foreach(string str in string2DArray) {
Console.WriteLine(str);
}
Console.WriteLine("-----------------------------");
Console.WriteLine("Jagged Array Sample");
int[][] intJaggedArray3 = {
new int[] {
2,
12
},
new int[] {
14,
14,
24,
34
},
new int[] {
6,
16,
26,
36,
46,
56
}
};
// Loop through all items of a jagged array
for (int i = 0; i < intJaggedArray3.Length; i++) {
Console.Write($"Element({i}): ");
for (int j = 0; j < intJaggedArray3[i].Length; j++) {
Console.Write($"{intJaggedArray3[i][j]} ");
}
Console.WriteLine();
}
Console.WriteLine("-----------------------------");
Listing 1
The output of Listing One looks like Figure 1.

Figure 1
The Array Class
Array class in C# is the mother of all arrays and provides functionality for creating, manipulating, searching, and sorting arrays in the .NET Framework.
Array class, defined in the System namespace, is the base class for arrays in C#. However, the Array class is an abstract base class, meaning we cannot create an instance of the Array class.
Creating an Array
Array class provides the CreateInstance method to construct an array. The CreateInstance method takes the first parameter as the type of items, and the second and third parameters are the dimension and their range. Once an array is created, we use the SetValue method to add items.
The following code snippet creates an array and adds three items to the Array. As you can see, the type of the array items is a string, and the range is 3. Therefore, you will get an error message if you try adding the 4th item to the Array.
Array stringArray = Array.CreateInstance(typeof(String), 3);
stringArray.SetValue("Mahesh Chand", 0);
stringArray.SetValue("Raj Kumar", 1);
stringArray.SetValue("Neel Beniwal", 2);
Calling SetValue on an existing item of an array overrides the previous item value with the new value.
The code snippet in Listing 2 creates a multi-dimensional array.
Array intArray3D = Array.CreateInstance(typeof(Int32), 2, 3, 4);
for (int i = intArray3D.GetLowerBound(0); i <= intArray3D.GetUpperBound(0); i++)
for (int j = intArray3D.GetLowerBound(1); j <= intArray3D.GetUpperBound(1); j++)
for (int k = intArray3D.GetLowerBound(2); k <= intArray3D.GetUpperBound(2); k++) {
intArray3D.SetValue((i * 100) + (j * 10) + k, i, j, k);
}
foreach(int ival in intArray3D) {
Console.WriteLine(ival);
}
Listing 2
Array Class Properties
Table 1 describes Array class properties.
| IsFixedSize | Return a value indicating if an array has a fixed size or not. |
| IsReadOnly | Returns a value indicating if an array is read-only or not. |
| LongLength | Returns a 64-bit integer representing the total number of items in an array's dimensions. |
| Length | Returns a 32-bit integer representing the total number of items in an array's dimensions. |
| Rank | Returns the number of dimensions of an array. |
The code snippet in Listing 3 creates an array and uses Array properties to display property values.
int[] intArray = new int[3] {
0,
1,
2
};
if (intArray.IsFixedSize) {
Console.WriteLine("Array is fixed size");
Console.WriteLine($"Size : {intArray.Length.ToString()}");
Console.WriteLine($"Rank : {intArray.Rank.ToString()}");
}
Listing 3
The output of the Listing looks like Figure 2.

Figure 2
Search an element in an Array
The BinarySearch static method of the Array class can be used to search for an item in an array. This method uses the binary search algorithm to search for an item. The method takes at least two parameters. The first parameter is the Array you would like to explore, and the second parameter is an object that is the item you are looking for. If an item is found in the Array, the method returns the index of that item (based on the first item as the 0th item). Otherwise, the process returns a negative value.
Note. You must sort an array before searching. See the comments in this article.
Listing 4 uses the BinarySearch method to search an array for a string.
// Create an array and add 5 items to it
Array stringArray = Array.CreateInstance(typeof(String), 5);
stringArray.SetValue("Mahesh", 0);
stringArray.SetValue("Raj", 1);
stringArray.SetValue("Neel", 2);
stringArray.SetValue("Beniwal", 3);
stringArray.SetValue("Chand", 4);
// Find an item
object name = "Neel";
int nameIndex = Array.BinarySearch(stringArray, name);
if (nameIndex >= 0) Console.WriteLine($"Item was at {nameIndex.ToString()}th position");
else Console.WriteLine("Item not found");
Listing 4
Sorting an Array
The Sort static method of the Array class can be used to sort array items. This method has many overloaded forms. The simplest form takes as a parameter the Array you want to sort. For example, Listing 5 uses the Sort method to sort array items. You can also sort a partial list of items using the Sort method.
// Create an array and add 5 items to it
Array stringArray = Array.CreateInstance(typeof(String), 5);
stringArray.SetValue("Mahesh", 0);
stringArray.SetValue("Raj", 1);
stringArray.SetValue("Neel", 2);
stringArray.SetValue("Beniwal", 3);
stringArray.SetValue("Chand", 4);
// Find an item
object name = "Neel";
int nameIndex = Array.BinarySearch(stringArray, name);
if (nameIndex >= 0) Console.WriteLine($"Item was at {nameIndex.ToString()}th position");
else Console.WriteLine("Item not found");
Console.WriteLine();
Console.WriteLine("Original Array");
Console.WriteLine("---------------------");
foreach (string str in stringArray)
{
Console.WriteLine(str);
}
Console.WriteLine();
Console.WriteLine("Sorted Array");
Console.WriteLine("---------------------");
Array.Sort(stringArray);
foreach (string str in stringArray)
{
Console.WriteLine(str);
}
Listing 5
The output of Listing 5 looks like Figure 3.

Figure 3
Alternatively, the Sort method takes the starting index and the number of items after that index. For example, the following code snippet sorts three items starting at the 2nd position.
Array.Sort(stringArray, 2, 3);
The new output looks like Figure 4.

Figure 4
Getting and Setting Values
The GetValue and SetValue methods of the Array class can be used to get and set values of an array's items. For example, the code listed in Listing 4 creates a 2-dimensional array instance using the CreateInstance method. After that, I use the SetValue method to add values to the Array.
Ultimately, I find several items in both dimensions and use the GetValue method to read values and display them on the console.
Array names = Array.CreateInstance(typeof(String), 2, 4);
names.SetValue("Rosy", 0, 0);
names.SetValue("Amy", 0, 1);
names.SetValue("Peter", 0, 2);
names.SetValue("Albert", 0, 3);
names.SetValue("Mel", 1, 0);
names.SetValue("Mongee", 1, 1);
names.SetValue("Luma", 1, 2);
names.SetValue("Lara", 1, 3);
int items1 = names.GetLength(0);
int items2 = names.GetLength(1);
for (int i = 0; i < items1; i++)
for (int j = 0; j < items2; j++)
Console.WriteLine($"{i.ToString()},{j.ToString()}: {names.GetValue(i, j)}");
Listing 6
The output of Listing 6 generates Figure 5.

Figure 5
Reverse an Array in C#
The Reverse static method of the Array class reverses the order of items in an array. Similar to the Sort method, you can pass an array as a parameter of the Reverse method.
Array stringArray = Array.CreateInstance(typeof(String), 5);
stringArray.SetValue("Mahesh", 0);
stringArray.SetValue("Raj", 1);
stringArray.SetValue("Neel", 2);
stringArray.SetValue("Beniwal", 3);
stringArray.SetValue("Chand", 4);
Console.WriteLine("Original Array");
Console.WriteLine("---------------------");
foreach(string str in stringArray) {
Console.WriteLine(str);
}
Console.WriteLine();
Console.WriteLine("Reversed Array");
Console.WriteLine("---------------------");
Array.Reverse(stringArray);
// Array.Sort(stringArray, 2, 3);
foreach(string str in stringArray) {
Console.WriteLine(str);
}
Console.WriteLine();
Console.WriteLine("Double Reversed Array");
Console.WriteLine("---------------------");
Array.Reverse(stringArray);
// Array.Sort(stringArray, 2, 3);
foreach(string str in stringArray) {
Console.WriteLine(str);
}
Listing 7
The output of Listing 7 generates Figure 6.

Figure 6
Clear an Array
The Clear static method of the Array class removes all items of an array and sets its length to zero. This method takes three parameters - first, an array object; second, the d starting index of the collection and third, the e number of elements. For example, the following code clears two elements from the Array starting at index 1 (which means the second element of the Array).
Array.Clear(stringArray, 1, 2);
Note. Keep in mind the Clear method does not delete items. Just clear the values of the items.
The code listed in Listing 8 clears two items from index 1.
Array stringArray = Array.CreateInstance(typeof(String), 5);
stringArray.SetValue("Mahesh", 0);
stringArray.SetValue("Raj", 1);
stringArray.SetValue("Neel", 2);
stringArray.SetValue("Beniwal", 3);
stringArray.SetValue("Chand", 4);
Console.WriteLine("Original Array");
Console.WriteLine("---------------------");
foreach (string str in stringArray)
{
Console.WriteLine(str);
}
Console.WriteLine();
Console.WriteLine("Clear Items");
Console.WriteLine("---------------------");
Array.Clear(stringArray, 1, 2);
foreach (string str in stringArray)
{
Console.WriteLine(str);
}
Listing 8
The output of Listing 8 generates Figure 7. As you can see from Figure 7, the values of two items from the output are missing, but the actual items are there.

Figure 7
Get the size of an array
The GetLength method returns the number of items in an array. The GetLowerBound and GetUppperBound methods return an array's lower and upper bounds, respectively. All these three methods take at least a parameter, which is the index of the dimension of an array. For example, the following code snippet uses all three methods.
Console.WriteLine(stringArray.GetLength(0).ToString());
Console.WriteLine(stringArray.GetLowerBound(0).ToString());
Console.WriteLine(stringArray.GetUpperBound(0).ToString());
Copy an Array
The Copy static method of the Array class copies a section of an array to another array. The CopyTo method copies all the elements of a variety to another one-dimension array. For example, the code listed in Listing Nine copies the contents of an integer array to an array of object types.
// Creates and initializes a new Array of type Int32.
Array oddArray = Array.CreateInstance(Type.GetType("System.Int32"), 5);
oddArray.SetValue(1, 0);
oddArray.SetValue(3, 1);
oddArray.SetValue(5, 2);
oddArray.SetValue(7, 3);
oddArray.SetValue(9, 4);
// Creates and initializes a new Array of type Object.
Array objArray = Array.CreateInstance(Type.GetType("System.Object"), 5);
Array.Copy(oddArray, oddArray.GetLowerBound(0), objArray, objArray.GetLowerBound(0), 4);
int items1 = objArray.GetUpperBound(0);
for (int i = 0; i < items1; i++)
Console.WriteLine(objArray.GetValue(i).ToString());
Listing 9
You can even copy a part of an array to another, bypassing the number of items and starting items in the Copy method. For example, the following format copies various items from an Array starting at the specified source index. Then, it pastes them to another Array starting at the selected destination index.
public static void Copy(Array, int, Array, int, int);
Clone an Array
The Clone method creates a shallow copy of an array. The references in the new Array point to the same objects as those in the original Array. A shallow copy of an Array copies only the elements of the Array, whether reference types or value types, but it does not copy the objects that the references refer to.
The following code snippet creates a cloned copy of an array of strings.
string[] clonedArray = (string[])stringArray.Clone();
Convert an Array to a list
You use the ToList method to convert a C# array to a list.
List<string> namesList = names.ToList();
Continue here: Convert an Array to a List in C# (c-sharpcorner.com)
Summary
In this tutorial, you learned how to create different types of arrays in C#, initialize them, and use them in your applications. You also learned the .NET Array class and its properties and methods. Finally, you also learned to use the Array class methods to search, sort, and do other operations on arrays.

Junaid AtariPosted Dec 4, 2024, 4:49 PM
Explained well
Ben JonesPosted Mar 28, 2023, 10:31 PM
Hi Mahesh, I'm moving from JS to C# and trying to understand difference you refer to a Dynamic array by my understanding is all arrays in C# are fixed? But then there is a isFizedSize method that to me has no purpose so maybe I'm missing something? Also can you tell me the difference between a C# List and a C# Single Dimension Array?
Bela SzaboPosted Sep 28, 2022, 7:24 PM
It is very,very useful info. Thanks.
Ganesh ChikhalikarPosted Aug 4, 2022, 6:01 AM
Nice Article and Thanks for the information
Susan LlewellynPosted Oct 21, 2020, 2:04 PM
So how do you declare/initialize the array if it sits on top of a set of blocks? for example I have a string[] array that I want to reuse in a set of blocks that varies depending on a parameter. so each initialization would be different. The VS compiler seems to be forcing me to re-declare the array in each block.
Rikam PalkarPosted Jun 3, 2020, 1:15 AM
Good read.
Stephanie ParnellPosted Feb 23, 2020, 2:55 PM
Very helpful article for this intro to C# student! I can't find any information (so far in my researching) on how to prevent the input of an int not included in the array. For example - program prompts to enter area code and area code is not an element of the array.
Dinesh GabhanePosted Nov 7, 2019, 8:27 AM
Very Nice Article
Onkar SharmaPosted Nov 2, 2019, 8:48 AM
Very Informative Article, thanks for sharing with us...
Sourav Kumar DasPosted Nov 2, 2019, 3:28 AM
Nice useful Article Sir.
Pankajkumar PatelPosted Sep 17, 2019, 11:45 PM
Nice article
Subin ThomasPosted Feb 21, 2019, 4:26 AM
Articles with examples makes it easy to understand nice
Željko PerićPosted Oct 5, 2018, 1:05 PM
Multidimensional array sort example is missing ?
Rushi MehtaPosted Sep 11, 2018, 6:21 AM
Explained Very well
Sudheshwer RaiPosted May 23, 2018, 1:18 AM
Complete answer for array. Thanks Sir
Satish Kumar VadlavalliPosted Mar 7, 2018, 2:56 AM
Very nice and neetly explanation
McCarthy NwosuPosted Feb 14, 2018, 4:27 AM
Thanks Mahesh...I have learnt more in my knowledge of using arrays
Sanwar RanwaPosted Feb 3, 2018, 3:34 AM
Useful article..................
Vipin MittalPosted Jan 20, 2018, 5:24 AM
Very helpful artical..
Laxmidhar SahooPosted Dec 2, 2017, 6:27 AM
A usefull array definations
Radhakrishnama Raju BalarajuPosted Jul 9, 2017, 1:33 PM
Very nice and neetly explanation .I learn so ...on arrays after seeing this article
Anil JhaPosted Jun 21, 2017, 11:07 PM
nice article.........
Parth MehtaPosted Jan 9, 2017, 11:20 AM
Very good explanation in simplest language.
Bikesh SrivastavaPosted Jan 9, 2017, 2:35 AM
Good explanation about array,I never seen like this.
Manju lata YadavPosted Jan 9, 2017, 12:48 AM
Its a great article in simplest language.
Mahesh ChandPosted Jan 8, 2017, 9:02 PM
Wow. This is 15 years old article but still relevant. I will plan to review it and update what have changed in latest versions of C#.
Joe WilsonPosted Jan 8, 2017, 7:32 AM
Well it is nice article thank you.
Naveen K MPosted Dec 14, 2016, 11:54 AM
Nice article and good update
Anil Kumar MurmuPosted Aug 24, 2016, 11:42 AM
Nice one. I always had a thought string[] strArray = new string[] { "Mahesh Chand", "Mike Gold", "Raj Beniwal", "Praveen Kumar", "Dinesh Beniwal" }; is a fixed array. Even though we don't mention the array size, but still we are not allowed to push more values into this array at run time. Now seems like I got something new to learn about array. I would like to thank you for your contribution to this topic.
Shamim UddinPosted Aug 18, 2016, 8:15 AM
Nice
Ramesh PalaniappanPosted Aug 18, 2016, 8:13 AM
Nice
kalu singh raoPosted Jul 7, 2016, 8:37 AM
Nice...
Mayank SharmaPosted Jun 27, 2016, 2:09 AM
Nice article.
RajaPosted Jun 1, 2016, 4:14 AM
Good One...
Nhat NguyenPosted May 21, 2016, 12:01 PM
My Code : long num = 0; object[,] array; array = (object[,])Array.Copy((Array)array, new object[13, (int)num + 1 ] , array . Length ); My error : Error 12 Cannot convert type 'void' to 'object[*,*]' Please help me… Thanks so much
Bhuvanesh MohankumarPosted May 8, 2016, 2:17 AM
Really good updation
Amit DavePosted Apr 29, 2016, 4:35 AM
Good one
Shamim UddinPosted Apr 21, 2016, 5:55 AM
Good one
Bhuvanesh MohankumarPosted Apr 19, 2016, 2:31 PM
Good one
malik junaidPosted Apr 14, 2016, 1:16 PM
helpful...
Vignesh ManiPosted Mar 21, 2016, 5:07 PM
Good one
Prashant VermaPosted Mar 10, 2016, 3:14 AM
Good one
Prashant VermaPosted Mar 10, 2016, 3:14 AM
Nice Article
Anil JhaPosted Mar 2, 2016, 4:49 AM
nice article
imran azamPosted Mar 2, 2016, 4:13 AM
easy to understand
Sonu ChaudharyPosted Feb 25, 2016, 6:28 AM
good artical
Asfend YarPosted Feb 21, 2016, 9:50 AM
Very helpful and valuable information in this article
Asfend YarPosted Feb 21, 2016, 9:50 AM
Nice share
Asfend YarPosted Feb 21, 2016, 9:49 AM
good
Shailesh UkePosted Feb 16, 2016, 1:53 AM
Nice Article
Sr KarthigaPosted Feb 10, 2016, 9:14 AM
Good one sir its very intresting
Ashish SrivastavaPosted Feb 4, 2016, 3:59 AM
NICE
Niranjan pandeyPosted Dec 31, 2015, 1:01 AM
Nice Sir
Joe WilsonPosted Dec 30, 2015, 2:08 AM
Thank you very much.
Rajesh SinghPosted Dec 27, 2015, 11:38 AM
well explained.
Chamuth ChamandanaPosted Dec 22, 2015, 2:15 AM
Good article...
Ankur MistryPosted Nov 28, 2015, 5:14 AM
Nice
Former memberPosted Nov 18, 2015, 1:44 PM
good one
Ali AhmedPosted Nov 1, 2015, 11:50 AM
This is the best article in C-sharpcorner.
Suman VermaPosted Oct 20, 2015, 8:35 AM
Nice article.
Mukesh KumarPosted Oct 17, 2015, 2:51 PM
Best article sir.. thanks for sharing with us
Anil Kumar MurmuPosted Oct 9, 2015, 4:33 AM
Nice article.
Mahesh ChandPosted Sep 9, 2015, 8:53 AM
Thank you guys. Appreciate all the feedback.
Baimey RajeshPosted Sep 9, 2015, 7:17 AM
@Mahesh you always do keep up the quality. Good job.
Ajeet MishraPosted Sep 1, 2015, 4:13 AM
Nice
Ratnesh SinghPosted Aug 26, 2015, 10:45 AM
Nice ...
Yashwanth MuthineniPosted Aug 25, 2015, 3:36 AM
Nice Share sir
Ahtasham HassanPosted Aug 22, 2015, 12:43 AM
well job
Govinda Rajulu YemineniPosted Jul 14, 2015, 5:08 AM
Nice Article Sir
Muralishankar SundaramPosted Jun 4, 2015, 1:04 AM
Nice Recap for Array concept....
Aditya manthankarPosted May 28, 2015, 6:48 AM
Nice Aricle
Shailesh UkePosted May 27, 2015, 10:13 AM
well done that's good
Md. Raskinur RashidPosted Jan 10, 2015, 12:15 PM
It's all about C# array. so no need to anywhere to understand. gr8!
praveen singhPosted Jan 8, 2015, 1:11 PM
Great Job
Shamsh TabrezPosted Dec 16, 2014, 10:34 AM
A perfect number is one that is the sum of its factors, excluding itself. The 1st perfect number is 6 because 6 = 1 + 2 + 3. The 2nd perfect number is 28 which equals 1 + 2 + 4 + 7 + 14. The third is 496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248. In each case, the number is the sum of all its factors excluding itself. Write a method named henry that takes two integer arguments, i and j and returns the sum of the ith and jth perfect numbers. So for example, henry (1, 3) should return 502 because 6 is the 1st perfect number and 496 is the 3rd perfect number and 6 + 496 = 502. The function signature is int henry (int i, int j) You do not have to worry about integer overflow, i.e., you may assume that each sum that you have to compute can be represented as a 31 bit integer. Hint: use modulo arithmetic to determine if one number is a factor of another. Please solve this.I need its solution.
Surya KantPosted Nov 9, 2014, 6:05 AM
All the information very help full ,but if u provide the concept more describly then it is good for any one understand it easily...Thanks.
computer columnsPosted Mar 28, 2013, 1:45 AM
Great Job. As we can say that c-sharp and cplusplus is the mother of programming. http://www.ccolumns.com
prathapPosted Oct 27, 2012, 2:42 AM
In C#, arrays are reference type, so the developer can have an array element refer to another array. When one array refers to another, you get a jagged array. .Net 4.0 does not support Mixed Array concept http://wisentechnologies.com/it-courses/.net-training.aspx
Mahesh ChandPosted Sep 10, 2012, 4:06 PM
Ibrahim, what exactly are you trying to do? You may not need array after all if you are using a Grid.
Ibrahim Imam muntakaPosted Sep 1, 2012, 12:35 AM
Hello Sir, I am extremely grateful for reading from your article. But my problem is, I don't know which array is the best to randomly display the values 0-8 in a grid of 3x3. And how do I do it.I have tried 2D array but to no avail. Please help.
Ibrahim Imam muntakaPosted Sep 1, 2012, 12:34 AM
Hello Sir, I am extremely grateful for reading from your article. But my problem is, I don't know which array is the best to randomly display the values 0-8 in a grid of 3x3. And how do I do it.I have tried 2D array but to no avail. Please help.
Biswarup GhoshPosted Aug 16, 2012, 3:16 AM
Hi Mital Sorathiya this the solution using System; namespace classobj { public class test { int a, b; public void get() { Console.Write("Enter a : "); a = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter b : "); b = Convert.ToInt32(Console.ReadLine()); } public void put() { Console.WriteLine("Addition : " + (a + b)); } } class Program { static void Main(string[] args) { test _test = new test(); int i; for (i = 0; i < 3; i++) { _test.get(); _test.put(); } Console.ReadKey(); } } }
Amin IslamPosted Jul 3, 2012, 11:08 AM
Accessing multi-dimensional arrays A multi-dimensional array items are represented in a matrix format and to access it's items, we need to specify the matrix dimension. For example, item(1,2) represents an array item in the matrix at second row and third column. The following code snippet shows how to access numbers array defined in the above code. Console.WriteLine(numbers[0,0]); Console.WriteLine(numbers[0, 1]); Console.WriteLine(numbers[1, 0]); Console.WriteLine(numbers[1, 1]); Console.WriteLine(numbers[2, 0]); Console.WriteLine(numbers[2, 2]); I am a vert elementry level C# student. Please review the last line,"Console.WriteLine(numbers[2, 2]);" this should have been,"Console.WriteLine(numbers[2, 1]);" please help me to clear the idea, behind the item in the matrix at second row and third column. Thank you for your responce. Thanks Amin
SIVAPosted May 2, 2012, 11:22 AM
Awesome article..
Mital SorathiyaPosted Feb 22, 2012, 12:30 AM
using System; namespace classobj { public class test { int a, b; public void get() { Console.Write("Enter a : "); a = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter b : "); b = Convert.ToInt32(Console.ReadLine()); } public void put() { Console.WriteLine("Addition : " + (a + b)); } } class Program { static void Main(string[] args) { test[] t = new test[3]; int i; for (i = 0; i < 3; i++) { t[i].get(); t[i].put(); } Console.ReadKey(); } } } find Error plz give ans
pritam maskePosted Dec 13, 2011, 8:26 AM
when we create an single dimmensional array ,in that we want to find perfect square number.then how we will find it??
VijayPosted Aug 23, 2011, 3:50 AM
Very good Article.
A FPosted Jun 5, 2011, 2:31 PM
So you show how to initiate arrays with names you already know. How do you get user input names INTO an array? string[] names = new string [100] So I don't know the names, I just know you can enter up to 100 strings. Now how do you get the names you enter from a method INTO THE STUPID array? You don't show that at all, which makes this article useless for anything practical other than displaying a set of pre-defined names.
gishnu tnPosted May 20, 2011, 9:30 AM
can i use try catch when i get values for user for an array, is that necessary,
ashok kumarPosted Feb 16, 2011, 11:06 PM
Good job..
Sam HobbsPosted Jan 13, 2011, 1:42 PM
This is an excellent article about arrays and I am sure it has helped many people. I apologize, however, for having to say this, but I agree with ababab on June 17, 2009. I would not be harsh as in that comment, but I agree that the Array class does not support dynamic arrays. If it does, I am very interrested in seeing examples of that. Also, what happens when an unsorted array is searched using BinarySearch? Is the array automatically sorted first? I assume not, so I assume it would help to state explicitly that an array must be sorted for the BinarySearch to work. Also note that the example of sorting an array is not an example of sorting an array.
rika mardianaPosted Dec 25, 2010, 5:28 AM
i want to make a database with array which is the output is a table that all my inputed will view in the table
John WolckotPosted Oct 25, 2010, 9:25 AM
Hi, Great script, I need to know what is the meaning of this line: int[][] numArray = new int[][] { new int[] {1,3,5}, new int[] {2,4,6,8,10} }; Thanks, John Wolcot Locksmith Austin
hamza sopariwalaPosted Jul 17, 2010, 4:29 AM
how to print the largest number of a dynamic array
domin hasanPosted May 10, 2010, 1:28 PM
please help me to make console project about "Hashing" Closed / Chained Addressing (use array not used linked list)...thanks
krish sharpPosted Feb 28, 2010, 8:50 AM
this is very useful to me.keep update. by regards, krishna
Loyiso gawulaPosted Feb 14, 2010, 2:15 PM
ABC Cookery class needs a program that will enable them to enter the marks of judges for the class’s student demos. There are 10 students. Each student has a unique number between 1 and 10(both numbers included). Each student is judged by 3 judges. The marks for the 3 judges are input on Form1. The three marks are loaded into the columns of a 2D array, JudgesMarks. Each row is a student and the columns are the 3 judges’ marks for that student.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Sachin GaikwadPosted Nov 28, 2009, 2:04 AM
hello friend i am Sachin, i am a beginner in .net , please tell me how access the drives present on the computer like c:,d: etc and also the subfolders.Please mail me the answer on my mail id : [email protected]
sandip kallePosted Nov 11, 2009, 12:42 AM
Can u give simple example with for loop for 2D array.. Sandip www.aikadajiba.blogspot.com
lunaticPosted Sep 29, 2009, 6:34 PM
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:%5CUsers%5CAditi%5CAppData%5CLocal%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!--[if gte mso 10]> <![endif]--> hi I am a newbie in programming, so not very comfortable with arrays, list and collections stuff, currently working on an application for which I need to read values from an array (the values of array are gathered from an excel data sheet) and assign them to each individual member of the list, its somewhat like this: first value of array is assigned to first member of list second value of array is assigned to second member of list and so on.. I am having issues building the logic and code to make this work.. Wonder if you can help
gaby aPosted Jul 1, 2009, 7:03 PM
hello, I need a little help. I want to set up a array of object, but I can not handle very well. public class inregistrare { public static int nr_crt; public inregistrare() { nr_crt++; } public static string data; public static int getnr_crt() { return nr_crt; } inregistare[] array=new inregistrare[100]; for (int i=0; i<=100; 1++) { inregistrare.nr_crt=s.Substring(2,6); inregistrare.data=...;//nevermind ??array[i]=inregistrare() ????? ;//here I don't know what to do in order to record all the information from inregistrare.nr_crt and inregistrare.data in array[i] } I'm just a beginner.. Thank you,
VadimPosted Jun 18, 2009, 4:24 AM
Hallo, i have my class and want to create an array of my class' objects. How can i do it? I can't initiate it as myclass[x] myarray = new myclass(par a, par b)[x]; Thanks
ababab abababPosted Jun 17, 2009, 3:17 PM
This article is WRONG! In C#, arrays can be declared as fixed length or dynamic. Fixed length array can stores a predefined number of items, while size of dynamic arrays increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined. For example, the following like declares a dynamic array of integers. int [] intArray; This is just plain wrong. The above is NOT a declaration for a dynamic array. The simplest way to get dynamic arrays in C# is with ArrayList. The Array class is NOT dynamically sizable. Perhaps the author was confused because Array implements the IList interface. However, see this: http://msdn.microsoft.com/en-us/library/system.array.isfixedsize(VS.71).aspx
zain almasriPosted Apr 15, 2009, 2:40 PM
Can someone provide the code for the array class in C?
smita smitaPosted Jan 6, 2009, 7:43 AM
Explain variable size arrays in c#
Steven MieropPosted Dec 11, 2008, 1:11 PM
Sorry if this posted twice. I am trying to figure out a way for my program to figure out how many letters where used in what ever word I write in a textbox. For example if I write the word CAB, I want to a messagebox to display or a label to display "You used A 1 time" "You used B 1 time" "You used C 1 time" "You used D 0 times" ..... I don't know how to set up the array to do this. If you could help this very lost student, I would geatly appreciate it!
lakshmi lakshmiPosted Nov 19, 2008, 5:49 PM
hello any body help me with this i need a logic of if i gave 250.50 in one textbox after clicking the button i should get the twohundread and fifty rupees and fifty paise in textbox2 i need logic please help me
periyasami sPosted Oct 1, 2008, 8:34 AM
very nice. Thank u
Chellammal APosted Mar 5, 2008, 10:20 PM
Toaacept the total number of array elements and values from the user.To arrange elements in ascending order
varun raoPosted Feb 28, 2008, 5:28 PM
how do u compare the equality of 2 diff arrays and also 2 diff strings?
varun raoPosted Feb 28, 2008, 5:18 PM
how do u compare the equality of 2 diff arrays and also 2 diff strings?
sanj gautamPosted Feb 25, 2008, 2:37 PM
i want to find ot the size of an array by using while loop as we are doing in C++ like while(str[i] != '\0') {i++;} this code is giving error of array out of bound even though my declared size is much more greater than entered array. plz help me why this is coming. can u tell me how length function is working in C#??????
Roel van DuneditedPosted Nov 8, 2007, 7:06 AMEdited Nov 8, 2007, 7:11 AM
Hello, I have a class with an array as one of the fields. I want properties for all the fields, but I don't know how to do this with the array. Example: public class Player { private string name; public Player(string name) { this.name = name } public string Name { get { return name; } } } public class Team { private int number; private Player[] players; public Team(int number) { this.number = number; this.players = new Player[10]; public Player Player { get { ??? } set { ??? } } } } I can get or set a whole array, but how do I do get a specific item of the array? I tried: public Player Player(int i) { get { return players.GetValue(i); } set { players.setValue(value, i); } } but that doesn't work. Can anyone plz help me?
pieter pattiruhuPosted Oct 19, 2007, 11:17 PM
dear author, my name is pieter...i'm newbie in C-sharp. here is my program list: for ( i = 0; i <= u; i++) { z[i]=a * z[i-1] + c % 16; console.writeline(z[i]); } please check my short list program( because the program doesnt work properly), if don't mind please tell me if i have make a mistake. thank you very much. thank you very much....God Bless... warm greeting from indonesia....
SatheesPosted Sep 4, 2007, 3:37 AM
I have created an array instance as global . I used command click event to load data to this array. Another command click i created to display the content of array. But when I click it says erro messge "Object reference not set to an instance of an object.". But when i try from the previous click function it works. Any help please
SatheesPosted Sep 4, 2007, 3:37 AM
I have created an array instance as global . I used command click event to load data to this array. Another command click i created to display the content of array. But when I click it says erro messge "Object reference not set to an instance of an object.". But when i try from the previous click function it works. Any help please
adminPosted Aug 31, 2007, 3:44 AM
nice tutorial but i want array of text box? How to create this in C sharp? please reply
alexPosted Aug 28, 2007, 10:04 AM
Hi! Is it possible to create an array with dinamyc size? Let say we are reading rows from DB table or lines from a file?
emani amaPosted Aug 7, 2007, 6:32 PM
how i can use the sorting (selection & exchanging) for these variables 16 8 12 21 24 11 25 in microsoft visual j++ 6.0 thnk you
kikiPosted Aug 5, 2007, 1:55 PM
how can I print the contents of a Multi Dimension Array like string[,] s=new string[20,3]; usining Response.Write() for showing the content?? thank you for your help
LIBERT TAPIAPosted May 18, 2007, 12:00 AM
HI NICE TUTORIAL HELP ALOT I AM NEW AT C# ANDWAS DOING A SOFTWARE THAT REQUIRES THE USER TO ENTER A SERIES OF DATA IN A TABLE (X & Y) AND WAS TRYING TO MAKE A CONTROL THAT A CREATED OF TWO TEXT BOX AND WHEN THE USER HTS THE ADD BUTTON A NEW CONTROL WILL APPEAR UNDER THE FIST ONE
abhijeet koratkarPosted May 7, 2007, 3:28 AM
i want code for following example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Tavleen OberoiPosted Apr 25, 2007, 4:03 PM
How can we pick words from richtextbox and put them in array of strings? For example if the text of richtextbox is hi , how are you ? then the string array contains {"hi" , "how" , "are" , "you" , "?" } please reply
Tavleen OberoiPosted Apr 25, 2007, 4:00 PM
How can we pick words from richtextbox and put them in array of strings? For example if the text of richtextbox is hi , how are you ? then the string array contains hi , how are you ? please reply
dolaPosted Nov 22, 2006, 12:56 AM
Hello, I read your article on arrays. Nice article Please can you help with codes on implementing a multidimensional array for a database that has 5 tables , and each table has 5 attributes and 12 records . How do i populate the tables using the code. Thanks
a wPosted Aug 30, 2006, 9:33 AM
What is the meaning of the following multidimensinal array declartion : int [ , , ] arr=new int[4,2,3]; please elaborate.