Skip to content

Instantly share code, notes, and snippets.

@Lahirutech
Last active May 22, 2021 13:17
Show Gist options
  • Save Lahirutech/8c4d0b72ab19745ffa05d3386fd71525 to your computer and use it in GitHub Desktop.
Save Lahirutech/8c4d0b72ab19745ffa05d3386fd71525 to your computer and use it in GitHub Desktop.
var myarray = new ArrayList()
{
1,
"Bill",
300,
4.5f
};
//Access individual item using indexer
int firstElement = (int) myarray[0]; //returns 1
string secondElement = (string) myarray[1]; //returns "Bill"
//int secondElement = (int) myarray[1]; //Error: cannot cover string to int
//using var keyword without explicit casting
var firstElement = myarray[0]; //returns 1
var secondElement = myarray[1]; //returns "Bill"
//var fifthElement = myarray[5]; //Error: Index out of range
//update elements
myarray[0] = "Steve";
myarray[1] = 100;
//myarray[5] = 500; //Error: Index out of range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment