在.NET Framework 4.0 中新增加了一些很方便的功能,比如:System.Dynamic支持動態語言運行時、System.Numerics.Complex 復數、System.Numerics.BigInteger 大數、System.Tuple 對象、遍歷文件夾下文件夾和文件的新方法Directory.EnumerateDirectories、Directory.EnumerateFiles、Directory.EnumerateFileSystemEntries等,詳細的新功能列表可以參考下面的鏈接:
http://msdn.microsoft.com/en-us/library/ms171868(VS.100).aspx
同時,還否定了一些原先的功能,這些過時的內容可以參考
http://msdn.microsoft.com/en-us/library/ee461502%28VS.100%29.aspx
下面就是.NET 4.0中新增加的String.IsNullOrWhiteSpace() 方法,方便用戶對字符串進行處理。
class TestNET4
{
static void Main()
{
String[] TestString = { null , String.Empty, " " , " " , " abc " , " /t " , " /r/n " , " /v " , " /f " , " /a " };
for ( int i = 0 ; i < TestString.Length; i ++ )
{
String temp = TestString[i];
if (temp == null )
{
Console.WriteLine( " null IsNullOrWhiteSpace = "
+ String.IsNullOrWhiteSpace(temp).ToString());
}
else
{
Console.WriteLine(temp + " Length= " + temp.Length.ToString()
+ " IsNullOrWhiteSpace = "
+ String.IsNullOrWhiteSpace(temp).ToString());
}
}
}
}
程序執行結果:
null IsNullOrWhiteSpace = True
Length=0 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = True
abc Length=4 IsNullOrWhiteSpace = False
Length=1 IsNullOrWhiteSpace = True
Length=2 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = True
Length=1 IsNullOrWhiteSpace = False
IsNullOrWhiteSpace方法的具體實現代碼為:
{
if (value != null )
{
for ( int i = 0 ; i < value.Length; i ++ )
{
if ( ! char .IsWhiteSpace(value[i]))
{
return false ;
}
}
}
return true ;
}
所以,他是通過判斷char.IsWhiteSpace方法來實現的,有些特殊字符也被當作空白字符,這一點特別注意注意,比如全角空格。
