在C# 中向该泛型集合中添加一个键为“李四”的学员对象以下是对其不同操作其正确的是( )选择二项)
来源:百度知道 编辑:UC知道 时间:2024/09/21 22:42:23
在C# 中有如下泛型集合代码,向该泛型集合中添加一个键为“李四”的学员对象,以下选项是对该泛型集合不同操作,其正确的是( )。 (选择二项)
Dictionary<string,Student> dict = new Dictionary<string,Student>();
a) dict.RemoveAt(0);
b) Student stu = dict[“李四”];
c) Foreach(Student stu in dict.Keys){…}
d) Foreach(Student stu in dict.Values){…}
Dictionary<string,Student> dict = new Dictionary<string,Student>();
a) dict.RemoveAt(0);
b) Student stu = dict[“李四”];
c) Foreach(Student stu in dict.Keys){…}
d) Foreach(Student stu in dict.Values){…}
B,D
A错在,Dictionary没有RemoveAt方法,只有
public bool Remove ( TKey key )方法
C错在Student stu in dict.Keys
这返回的明明是字符串。
Dictionary<(Of <(TKey, TValue>)>)
TKey
字典中的键的类型。
TValue
字典中的值的类型。
而你给的例子中
TKey是string
TValue是Student类
所以遍历的时候,要用d,才是正确的,c要用string类型
既然键值为李四(string)
那自然就可以用dict[“李四”];
来获取这个对象了
B 跟 D
肯定是对的
C