Introducing .NET 4.0 With VS 2010 读书笔记.2
Changes to Existing Functionality
Path.Combine()
The Path.Combine() method has new overloads that accept three and four parameters and an array of strings.
Stream.CopyTo()
Stream.CopyTo() allows you to copy the contents of one stream to another, avoiding some tedious coding:
MemoryStream destinationStream = new MemoryStream();
using (FileStream sourceStream = File.Open(@"c:\temp.txt", FileMode.Open))
{
sourceStream.CopyTo(destinationStream);
}
Enum.HasFlag()
Returns a Boolean value indicating whether one or more flags are set on an enum. For example, you
could use the HasFlag() method to test whether a car has particular options set:
[Flags]
publicenum CarOptions
{
AirCon =1,
Turbo =2,
MP3Player =4
}
staticvoid Main(string[] args)
{
CarOptions myCar = CarOptions.MP3Player | CarOptions.AirCon | CarOptions.Turbo;
Console.WriteLine("Does car have MP3? {0}", myCar.HasFlag(CarOptions.MP3Player));
Console.ReadKey();
}
String.IsNullOrWhiteSpace()
Detects if a string is null, empty, or consists of whitespace characters (avoiding a call to Trim()):
String.IsNullOrWhiteSpace("");
StringBuilder.Clear
Removes content from the StringBuilder object(essentially the same as setting a string builder’s length to 0, but with a more readable syntax):
StringBuilder sb =new StringBuilder("long string");
sb.Clear();
Stopwatch.Restart()
Stops the recording of the current time period and starts a new one:
var sw =new Stopwatch();
sw.Start();
sw.Restart();

浙公网安备 33010602011771号