文档库 最新最全的文档下载
当前位置:文档库 › C#第3版第5章习题解答

C#第3版第5章习题解答

C#第3版第5章习题解答
C#第3版第5章习题解答

Ch5 泛型与LINQ 习题解答

1.假设Node类的每一个节点包括有两个字段:m_data(引用节点的数据)和m_next(引用链接列表中的下一项),这两个字段都是由构造函数方法设置的。该类有两个功能,第1个功能是通过名为Data和Next的只读属性访问m_data和m_next字段,第2个功能是对System.Object的ToString虚拟方法进行重写。试分别用类和泛型两种方法编写程序实现上述功能。

【解答】

using System;

class Node

{

Object m_data;

Node m_next;

public Node(Object data, Node next)

{

m_data = data;

m_next = next;

}

//访问结点数据

public Object Data

{

get { return m_data; }

}

//访问下一个结点

public Node Next

{

get { return m_next; }

}

// 获取结点数据描述

public override String ToString()

{

return m_data.ToString();

}

}

//链表结点类的泛型定义

class Node

{

T m_data;

Node m_next;

public Node(T data, Node next)

{

m_data = data;

m_next = next;

}

// 访问结点数据

public T Data

{

get { return m_data; }

set { m_data = value; }

}

// 访问下一个结点

public Node Next

{

get { return m_next; }

set { m_next = value; }

}

// 获取结点数据描述

public override String ToString()

{

return m_data.ToString();

}

}

// 使用结点类型或泛型结点类型

class LinkedList

{

static void Main(string[] args)

{

//// 创建整数链表

//Node head = new Node(5, null);

//head = new Node(10, head);

//head = new Node(15, head);

////遍历链表求整数和

//Int32 sum = 0;

//for (Node current = head; current != null;

// current = current.Next)

//{

// sum += (Int32)current.Data;

//}

//// 输出结果

//Console.WriteLine("Sum of nodes = {0}", sum);

// 用泛型创建整数链表

Node head = new Node(5, null);

head = new Node(10, head);

head = new Node(15, head);

// 遍历求和

Int32 sum = 0;

for (Node current = head; current != null;

current = current.Next)

{

sum += current.Data;

}

// 输出

Console.WriteLine("Sum of nodes = {0}", sum.ToString());

}

}

2. 编写程序创建一个的排序列表,向其添加5个元素后,按逆序方式显示列表中每一项的value值(string类型的值).

【解答】

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication

{

class Program

{

static void Main(string[] args)

{

SortedList list = new SortedList();

list.Add(10, "str10");

list.Add(2, "str2");

list.Add(13, "str13");

list.Add(24, "str24");

list.Add(15, "str15");

for (int i = list.Keys.Count - 1; i >= 0; i--)

{

Console.WriteLine(list[list.Keys[i]]);

}

Console.ReadLine();

}

}

}

3. 使用LINQ查询有哪些优势?什么是LINQ的延迟执行和立即执行?

【解答】

LINQ是一组查询技术的统称。其主要思想是将各种查询功能直接集成到C#语言中,不论是对象、XML还是数据库,都可以用C#语法编写查询语句。换言之,利用LINQ查询数据源就像用C#使用类、方法、属性和事件一样,完全用C#语法来构造,并具有完全的类型检查和智能提示(IntelliSense)支持。

LINQ执行查询时,一般利用foreach循环执行查询得到一个序列,这种方式称为“延迟执行”。对于聚合函数,如Count、Max、Average、First,由于返回的只是一个值,这类查询在内部使用foreach循环实现,而开发人员只需要调用LINQ提供的对应方法即可,这种方式称为“立即执行”。

4.创建一个List列表,向列表中添加10个元素,分别使用LINQ、lambda表达式两种方式查找列表中的偶数,并在控制台输出。

【解答】

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication

{

class Program

{

static void Main(string[] args)

{

List intList = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Console.WriteLine("使用LINQ查询列表:");

var query1 = from t in intList

where t % 2 == 0

select t;

foreach (var m in query1)

Console.Write(m + " ");

Console.WriteLine();

Console.WriteLine("使用Lamdam查询列表:");

var query2 = intList.Where(x => x % 2 == 0);

foreach (var m1 in query2)

Console.Write(m1 + " ");

Console.ReadLine();

}

}

}

THANKS !!!

致力为企业和个人提供合同协议,策划案计划书,学习课件等等

打造全网一站式需求

欢迎您的下载,资料仅供参考

相关文档