public IEnumerable<Order> GetOrders() { var orders = new List<Order>(); var con = new SqlConnection("some connection string"); var cmd = new SqlCommand("select * from orders", con); var rs = cmd.ExecuteReader(); while (rs.Read()) { // ... } rs.Dispose(); cmd.Dispose(); con.Dispose(); return orders; }
public IEnumerable<Order> GetOrders() { var orders = new List<Order>(); using (var con = new SqlConnection("some connection string")) { using (var cmd = new SqlCommand("select * from orders", con)) { using (var rs = cmd.ExecuteReader()) { while (rs.Read()) { // ... } } } } return orders; }
上面的代码中的using嵌套了好几层,看起来很繁琐,而且可读性也不是很好,我们可以像下面这样改进
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public IEnumerable<Order> GetOrders() { var orders = new List<Order>();
using (var con = new SqlConnection("some connection string")) using (var cmd = new SqlCommand("select * from orders", con)) using (var rs = cmd.ExecuteReader()) { while (rs.Read()) { // ... } } return orders; }
public class XmlUtility { public string ToXml(object input) { var xs = new XmlSerializer(input.GetType()); using (var memoryStream = new MemoryStream()) using (var xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding())) { xs.Serialize(xmlTextWriter, input); return Encoding.UTF8.GetString(memoryStream.ToArray()); } } }
上面的是很典型的XML序列化代码,但是我们在使用时需要先实例化这个类的对象,然后用对象来调用方法
1 2
var xmlUtil = new XmlUtility(); string result = xmlUtil.ToXml(someObject);
} public static string ToXml(object input) { var xs = new XmlSerializer(input.GetType()); using (var memoryStream = new MemoryStream()) using (var xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding())) { xs.Serialize(xmlTextWriter, input); return Encoding.UTF8.GetString(memoryStream.ToArray()); } } }
public static class XmlUtility { public static string ToXml(object input) { var xs = new XmlSerializer(input.GetType()); using (var memoryStream = new MemoryStream()) using (var xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding())) { xs.Serialize(xmlTextWriter, input); return Encoding.UTF8.GetString(memoryStream.ToArray()); } } }
public struct Point { public int X { get; set; } public int Y { get; set; } }
普通初始化如下
1 2 3
var startingPoint = new Point(); startingPoint.X = 5; startingPoint.Y = 13;
使用初始化器初始化
1
var startingPoint = new Point() { X = 5, Y = 13 };
代码的确精简了不少,从三行减到了一行,可以让我们少敲很多字。
下面再来看看集合的初始化,假设我们在一个集合List中添加5个整数
1 2 3 4 5
var list = new List<int>(); list.Add(1); list.Add(7); list.Add(13); list.Add(42);
使用集合初始化器,代码如下
1
var list = new List<int> { 1, 7, 13, 42 };
如果我们事先知道要加载的数量,可以给List设置默认的容量值,如下
1
var list = new List<int>(4) { 1, 7, 13, 42 };
下面来看一个通常情况下对象和集合一起使用的例子
1 2 3 4 5 6 7 8 9 10 11 12 13
var list = new List<Point>(); var point = new Point(); point.X = 5; point.Y = 13; list.Add(point); point = new Point(); point.X = 42; point.Y = 111; list.Add(point); point = new Point(); point.X = 7; point.Y = 9; list.Add(point);
下面为使用了初始化器的代码,可以对比一下区别
1 2 3 4 5 6
var list = new List<Point> { new Point { X = 5, Y = 13 }, new Point { X = 42, Y = 111 }, new Point { X = 7, Y = 9 } };