1 string.IsNullOrEmpty() and string.IsNullOrWhiteSpace()
{
if (fullPathFileName == null || fullPathFileName.Length == 0)
{
throw new ArgumentNullException(fullPathFileName);
}
//...
}
{
if (string.IsNullOrEmpty(fullPathFileName))
{
throw new ArgumentNullException(fullPathFileName);
}
//...
}
{
if (middleName == null || middleName.Trim().Length == 0)
{
return string.Format("{0} {1}", firstName, lastName);
}
return string.Format("{0} {1} {2}", firstName, middleName, lastName);
}
{
if (string.IsNullOrWhiteSpace(middleName))
{
return string.Format("{0} {1}", firstName, lastName);
}
return string.Format("{0} {1} {2}", firstName, middleName, lastName);
}
2 string.Equals()
{
if (orderType.Equals("equity"))
{
}
// ...
}
3 using语句
{
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;
}
{
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataReader rs = null;
var orders = new List<Order>();
try
{
con = new SqlConnection("some connection string");
cmd = new SqlCommand("select * from orders", con);
rs = cmd.ExecuteReader();
while (rs.Read())
{
// ...
}
}
finally
{
rs.Dispose();
cmd.Dispose();
con.Dispose();
}
return orders;
}
{
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;
}
{
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;
}
4 静态类(Static)
{
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());
}
}
}
string result = xmlUtil.ToXml(someObject);
{
private 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 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());
}
}
}
5 对象和集合初始化器
{
public int X { get; set; }
public int Y { get; set; }
}
startingPoint.X = 5;
startingPoint.Y = 13;
list.Add(1);
list.Add(7);
list.Add(13);
list.Add(42);
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);
{
new Point { X = 5, Y = 13 },
new Point { X = 42, Y = 111 },
new Point { X = 7, Y = 9 }
};
{
public static List<int> ThisList = new List<int>() { 1, 2, 3, 4, 5 };
}
public class NotBeforeFieldInit
{
public static List<int> ThisList;
static NotBeforeFieldInit()
{
ThisList = new List<int>();
ThisList.Add(1);
ThisList.Add(2);
ThisList.Add(3);
ThisList.Add(4);
ThisList.Add(5);
}
}
extends [mscorlib]System.Object
{
} // end of class BeforeFieldInit
.class public auto ansi NotBeforeFieldInit
extends [mscorlib]System.Object
{
} // end of class NotBeforeFieldInit