CreateInstance (Static Method In Array Class)
Syntax for Create Instance Method is :- CreateInstance(typeof(datatypename),length)
CreateInstance method create an instance of an Array class. Array is an abstract class so we cannot use "new" keyword to create an instance of an Array class.
--------------------------------------------------------------------------------------------------------
using System;
class CreateInstanceMethod
{
static void Main()
{
Array arr = Array.CreateInstance(typeof(int),5);
for(int i =0;i<arr.Length;i++)
{
arr.SetValue(i+10,i);
/*Instance member of an Array class to set value at index. we cannot use expression like arr[i] = 10 when create an instance with CreateInstance method.*/
}
for(int i =0;i<arr.Length;i++)
{
Console.WriteLine("Element in array is " + arr.GetValue(i));
//Instance member of an Array class to get value from the specified index
}
}
}
--------------------------------------------------------------------------------------------------------
CreateInstance (Static Method In Array Class)
Reviewed by Baljeet Singh
on
07:30
Rating:
No comments: