C#通过字符串而不是整数引用集合中的引用对象

Cha*_*ams 5 c# collections list

我有一个存储在List中的对象集合.

我想通过使用字符串名称而不是整数来解决对象.

List<Foo> fooList = new List<Foo>;
Foo item = fooList["Foo Name"]
Run Code Online (Sandbox Code Playgroud)

代替:

List<Foo> fooList = new List<Foo>;
Foo item = fooList[0]
Run Code Online (Sandbox Code Playgroud)

我在想我需要创建一个继承自List的集合类,但从那里开始,我不确定.

Jor*_*ren 14

Dictionary<string, Foo> 是你想要的类型.