Enumerable in C#——内迭代与外迭代示例
昨晚家里上不了网,打开以前下的视频:what's new in csharp 3.0 ?.感叹于VS开发环境的中Code Snippet,以及 CSharp3.0 对迭代的支持。
随手仿照视频写了一个代码,用来演示CSharp3.0对迭代的支持。
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
6
namespace EnumerableTest
7
{
8
class Customer
9
{
10
public string CustomerID { get; set; }
11
public string ContactName { get; set; }
12
public string City { get; set; }
13
}
14
15
16
class Program
17
{
18
static void Main(string[] args)
19
{
20
List<Customer> customers = LoadCustomers();
21
22
Outer Iteration
42
43
44
Inner Iteration
62
63
64
LINQ,对迭代的SQL Syntax式支持,我不太喜欢,还是感觉函数式编程更爽些,更直观些
75
76
77
Console.ReadKey();
78
}
79
80
private static void SeparatorLine()
81
{
82
Console.WriteLine();
83
}
84
85
private static void PrintCustomer(Customer cust)
86
{
87
Console.WriteLine("ID:{0} Contact:{1} City:{2}", cust.CustomerID.PadRight(10), cust.ContactName.PadRight(10), cust.City);
88
}
89
90
private static List<Customer> LoadCustomers()
91
{
92
List<Customer> customers = new List<Customer>()
93
{
94
new Customer(){ CustomerID="Andrew", ContactName="Anco", City="Hefei"},
95
new Customer(){ CustomerID="Nortel", ContactName="Anco", City="Shanghai"},
96
new Customer(){ CustomerID="Cisco", ContactName="Candy", City="Beijing"},
97
new Customer(){ CustomerID="HP", ContactName="Peter", City="Suzhou"},
98
new Customer(){ CustomerID="Lenovo", ContactName="Linda", City="Suzhou"}
99
};
100
return customers;
101
}
102
}
103
}
104
using System;2
using System.Collections.Generic;3
using System.Linq;4
using System.Text;5

6
namespace EnumerableTest7
{8
class Customer9
{10
public string CustomerID { get; set; }11
public string ContactName { get; set; }12
public string City { get; set; }13
}14

15

16
class Program17
{18
static void Main(string[] args)19
{20
List<Customer> customers = LoadCustomers();21

22
Outer Iteration42

43

44
Inner Iteration62

63

64
LINQ,对迭代的SQL Syntax式支持,我不太喜欢,还是感觉函数式编程更爽些,更直观些75

76

77
Console.ReadKey();78
}79

80
private static void SeparatorLine()81
{82
Console.WriteLine();83
}84

85
private static void PrintCustomer(Customer cust)86
{87
Console.WriteLine("ID:{0} Contact:{1} City:{2}", cust.CustomerID.PadRight(10), cust.ContactName.PadRight(10), cust.City);88
}89

90
private static List<Customer> LoadCustomers()91
{92
List<Customer> customers = new List<Customer>() 93
{ 94
new Customer(){ CustomerID="Andrew", ContactName="Anco", City="Hefei"},95
new Customer(){ CustomerID="Nortel", ContactName="Anco", City="Shanghai"},96
new Customer(){ CustomerID="Cisco", ContactName="Candy", City="Beijing"},97
new Customer(){ CustomerID="HP", ContactName="Peter", City="Suzhou"},98
new Customer(){ CustomerID="Lenovo", ContactName="Linda", City="Suzhou"}99
};100
return customers;101
}102
}103
}104



浙公网安备 33010602011771号