I'm wondering if the index of an array can be given a name in C# instead of the default index value. What I'm basically looking for is the C# equivalent of the following PHP code:
$array = array(
"foo" => "some foo value",
"bar" => "some bar value",
);
Cheers.
Arrays in C are indexed starting at 0, as opposed to starting at 1. The first element of the array above is point[0]. The index to the last value in the array is the array size minus one.
Elements of an array are accessed by specifying the index ( offset ) of the desired element within square [ ] brackets after the array name. Array subscripts must be of integer type. ( int, long int, char, etc. ) VERY IMPORTANT: Array indices start at zero in C, and go to one less than the size of the array.
Actually, it has very much to do with the question (specifically, yes, you CAN use a string as an index, but not in the obvious way that the original querier wants).
index—An array element's position number, also called a subscript. An index either must be zero, a positive integer, or an expression that evaluates to zero or a positive integer. If an application uses an expression as an index, the expression is evaluated first to determine the index.
PHP blends the concept of arrays and the concept of dictionaries (aka hash tables, hash maps, associative arrays) into a single array
type.
In .NET and most other programming environments, arrays are always indexed numerically. For named indices, use a dictionary instead:
var dict = new Dictionary<string, string> {
{ "foo", "some foo value" },
{ "bar", "some bar value" }
};
Unlike PHP's associative arrays, dictionaries in .NET are not sorted. If you need a sorted dictionary (but you probably don't), .NET provides a sorted dictionary type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With