您的位置:首页 > 编程语言 > C#

测测你对C#概念性的东西了解多少

2012-02-09 13:41 274 查看
1.在C#中,关于ArrayList和Hashtable的说法,错误的是()
A ArrayList通过索引访问集合元素,Hashtable通过Key访问集合元素
B ArrayList和Hashtable 都可以用RemoveAt方法删除其中的元素
C ArrayList和Hashtable获取集合中的元素时,都需要类型转换
D 在同一个ArrayList或者Hashtable中,可以存储不同类型的元素

2.下面关于泛型集合List<String> list = new List<String>()的操作代码错误的是()
A list.Remove(0)
B list.RemoveAt("王五")
C string name = list[0]
D string name = list["李四"]

3.下面关于泛型集合Dictionary<String, Student> dict = new Dictionary<string,Student>()
A dict.RemoveAt(0)
B foreach(Diction<string,Student> stu in dict){}
C foreach(Student stu in Keys){}
D foreach(Student stu in Values){}

4.在C#中,关于List<T>和Dictionary<K,V>的说法,正确的是()
A List<T>和Dictionary<K,V>都可以循环遍历整个元素对象
B 获取元素时,List<T>需要类型转换,Dictionary<K,V>不需要
C List<T>通过索引访问集合元素,Dictionary<K,V>通过Key访问集合元素
D 在同一个List<T>和Dictionary<K,V>中,可以存储不同类型的元素

5.分析下面一段程序,下列代码初始化对象错误的是()

public class Table
{
private int width;
private int height;
private string id;

public Table(int width,int height, string id)
{
this.width = width;
this.height = height;
this.id = id;

}

public Table(int height)
{
this.height = height;
}

public Table(int height,int width)
{
this.height = height;
this.width = width;
}
}

A Table myTable = new Table(10,10, "table")
B Table myTable = new Table(10,10)
C Table myTable = new Table(10)
D Table myTable = new Table()

6.指出下面代码的不合理之处,并予以改正

static void Main(string [] args)
{
Student stu1 = new Student("张三");
Student stu2 = new Student("李四");
Student stu3 = new Student("王五");
Dictionary<String,Student> dict = new Dictionary<String,Student>();
dict.Add(stu1.name,stu1);
dict.Add(stu2.name,stu2);
dict.Add(stu3.name,stu3);

//删除第二个元素
dict.RemoveAt(1);

//获取第一个元素
Student student = (Student)dict[stu1.name];

//遍历整个集合
foreach(Student stu in dict.Values)
{
Student myStudent = dict.Value;
}

}

1.B
2.ABD
3.CD
4.AC
5.D
6. 错误的是 //删除第二个元素 dict.RemoveAt(1); 是根据Key值来删除元素的,而不是根据索引删除元素 //获取第一个元素 Student student = (Student)dict[stu1.name]; 泛型不需要类型转换吧?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: