Array that can be accessed using array['Name'] in C#

前端 未结 3 1329
心在旅途
心在旅途 2021-02-08 00:38

Can you do

array[\'Name\'];

In C#

Rather than:

array[0];

I know you can do that in PHP but is there a

3条回答
  •  你的背包
    2021-02-08 01:11

    it's called a Dictionary in C#. Using generics you can actually index by any type. Like so:

    Dictionary dictionary = new Dictionary();
    Person myPerson = new Person(); 
    dictionary[myPerson] = "Some String";
    ...
    string someString = dictionary[myPerson];
    Console.WriteLine(someString);
    

    This obviously prints, "Some String" to the console.

    This is an example of the flexibility of the dictionary. You can do it with a string as an index too, like you asked for.

提交回复
热议问题