Arrays
Arrays are a special type, built into C#, that holds a group of other types. Arrays are a useful construct for organizing data. They provide matrix support and, even further, multidimensional support. As a collection, an array allows going beyond simple storage of data and provides fundamental discovery and manipulation of that data. Array methods are discussed in the next chapter. This chapter shows declaration and instantiation of single-dimension, multidimension, and jagged arrays.
Single-Dimension Arrays
Single-dimension arrays provide the capability to store and manipulate a list of items. Every element is of the same type. This is how such an array should be declared:
Type[] Array-Identifier [initializer] ;
The array identifier is any valid identifier. It should be meaningful for the purpose of the array. The optional initializer allocates memory for the array.
TIP
Once a C# array has been initialized, it can't change its size. If dynamic resizing of an array is needed, use one of the collection classes.
Here are some examples of single-dimensional array declarations:
// uninitialized declaration
MyClass[] myArray;
byte[] inputBuffer = new byte[2048];
// creates an array of 3 strings
string[] countStrings = { "one", "two", "three" };
Arrays may be declared with no initialization. Remember, an array must be initialized before it's used. It may be initialized with an integer type value inside the brackets of the initializer, as the following example shows:
// creates an array of 3 strings
string[] countStrings = new string[3] { "one ", " two", " three" };
Another way to initialize an array is by leaving the space in the brackets of the initializer blank and then following the initializer brackets with an initializer list in braces. The array initializer list is a comma-separated list of values of the array type. The size of the array becomes the number of elements in the initializer list. If an integer value is added to the initializer brackets and there is an initializer list in the same array initializer, make sure that the integer value in the initializer brackets is greater than or equal to the number of elements in the initializer list. Take a look at this code sample:
// error
string[] countStrings = new string[3] { "one", "two", "three", "four" };
The initializer in this code fails with an error because the allocated size of the countStrings array is only 3, but the number of strings in the list is four. The number of strings in the list can't exceed the allocated size.
TIP
Don't specify a size when using an initializer list on an array. Later, it will be easier to add an item to the list and avoid the mistake of not incrementing the number.
Dimensional Arrays
Multidimensional arrays are similar in declaration and initialization to single-dimensional arrays, with a few exceptions. For every new dimension included, add a comma to brackets of the array declaration. During initialization, add an integer value to each of the dimensions specified in the declaration. Here are a couple examples:
long [ , ] determinant = new long[4, 4];
int [ , , ] stateSpace = new int[2, 5, 4];
bool [ , ] exclusiveOr
= new bool[2, 2] { {false, true}, {true, false} };
Remember, the integer values in the initializer brackets are optional when including an initializer list.
Jagged Arrays
Jagged arrays allow creation of multidimensional arrays without the requirement of making every element of every dimension the same size. If an application has data that doesn't cover the entire range of possible values, this may be an option. It may also open the opportunity to save memory space. Here are some examples:
decimal[][] monthlyVariations = new decimal[12][];
monthlyVariations[(int)Months.Jan] = new decimal[31];
monthlyVariations[(int)Months.Feb] = new decimal[28];
.
.
.
monthlyVariations[(int)Months.Dec] = new decimal[31];
Kaydol:
Kayıt Yorumları (Atom)
BlackListIP control on Serenity platform (.NET Core)
In the Serenity platform, if you want to block IPs that belong to people you do not want to come from outside in the .net core web project,...
-
In the Serenity platform, if you want to block IPs that belong to people you do not want to come from outside in the .net core web project,...
-
TABLO ADI AÇIKLAMA L_PERSONEL Çalışma Alanı Tanımları L_SYSLOG Kullanıcı Kaydı İzleme L_L...
-
Microsoft OLE DB Provider for SQL Server : Cannot create a row of size 8100 which is greater than the allowable maximum of 8060. (80040E14) ...
Hiç yorum yok:
Yorum Gönder