Aslam-o-Alaikum
Can we fixed the generic collections as array..............?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Feb 22, 2011, 10:13 PM
Amit ChoudharyPosted Feb 22, 2011, 4:12 AM
you can create your own custom array using the Array class see the below example:
Creating an Array
Array class provides the CreateInstance method to construct an array. The CreateInstance method takes first parameter as the type of items and second and third parameters are the dimension and their range. Once an array is created, we use SetValue method to add items to an array.
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 string and range is 3. You will get an error message if you try to add 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);
Note: 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);
}Source Article: http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingWithArrays11232005064036AM/WorkingWithArrays.aspx
Thanks
Jaganathan BantheswaranPosted Feb 22, 2011, 3:49 AM
Can you ask the question clearly. So that we can understand better and will clear your doubt.