In this tutorial I will show you how can we store different types in An Array using C#
int[] arr = new int[3];
arr[0] = 0;
arr[1] = "String"; <---- Not Allowed because Array type in Int and we are storing String
Now in order to store different types we will need to make an Array of type Object, because all types are inherited from Object hence we can store any type in Array
object[] OArr = new object[3];
OArr[0] = 1;
OArr[1] = "String"; // <----- We can store string because Array type is Object
We can also stored Class object in this Array
I hope it was informative for you and I would like to Thank you for reading.
int[] arr = new int[3];
arr[0] = 0;
arr[1] = "String"; <---- Not Allowed because Array type in Int and we are storing String
Now in order to store different types we will need to make an Array of type Object, because all types are inherited from Object hence we can store any type in Array
object[] OArr = new object[3];
OArr[0] = 1;
OArr[1] = "String"; // <----- We can store string because Array type is Object
We can also stored Class object in this Array
I hope it was informative for you and I would like to Thank you for reading.
No comments:
Post a Comment