5 Wrong ways to check empty strings五誤區的方法來檢查空字符串
It is one of the common mistake that people compare a string with “” or String.Empty in VB.Net or C# to find its empty.這是一個常見的錯誤,人們比較字符串“ ”或String.Empty在VB.Net或C #中找到自己的空白。 Here are few examples.這裡有幾個例子。
// C# Wrong Ways / / C #中錯誤的途徑
- if ( s == “” )如果(星期日== “ ” )
- if ( s == string.Empty )如果(星期日== string.Empty )
- if ( s.Equals(””) )如果( s.Equals ( “ ” ) )
- if ( s.Equals ( String.Empty)如果( s.Equals ( String.Empty )
- if ( string.Equals(s,””)如果( string.Equals (星期日, “ ” )
- if ( string.Equals ( s,String.Empty ))如果( string.Equals (星期日, String.Empty ) )
So what’s the correct way to do it ?那麼,正確地做到這一點嗎? Check for length too.檢查長也。
// [ C# ] Correct Way / / [ C #中]正確途徑
if ( s.Length == 0 )如果( s.Length == 0 )
This is in continuation of our last post on這是我們繼續上次職位 Check for length too when you check for null strings in VB.Net and C#檢查長度也當您檢查是否為NULL字符串VB.Net和C #
Read below for the right approach.閱讀下面的正確的方法。
Tags:標籤: check empty strings檢查空字符串 , , empty strings空字串

Posted on 6th April 2008 by發布於08年4月6日由 Ashish Mohta Ashish Mohta , A tech blogger who writes about solving day to day problems of people who use computer.一個高科技博客誰寫了關於解決日常問題的人誰使用計算機。 He also writes on How to use the applications like Office, PC tips, Online tools,Browsers and more. All posts by他在書中還對如何使用應用軟件,如辦公室,電腦提示,在線工具,瀏覽器和更多。 所有職位 Ashish Mohta Ashish Mohta | Connect with me @ |與我聯繫@ Twitter 嘰嘰喳喳 | | Linkedin LinkedIn的 | | Facebook 臉譜 | | Stumble 失敗 | Need more help? |需要更多的幫助? Ask your Questions at our 問問你的問題在我們的 Support Center 支援中心






























Free Email Subscription


If you look at the disassembly of string.IsNullOrEmpty you will see what it does.如果你看一下拆卸string.IsNullOrEmpty你會看到他們做的。
.method public hidebysig static bool IsNullOrEmpty(string ‘value’) cil managed 。 hidebysig方法公共靜態布爾IsNullOrEmpty (字符串'價值' )舉行非正式協商管理
{ (
.maxstack 8 。 maxstack 8日
L_0000: ldarg.0 L_0000 : ldarg.0
L_0001: brfalse.s L_000d L_0001 : brfalse.s L_000d
L_0003: ldarg.0 L_0003 : ldarg.0
L_0004: callvirt instance int32 System.String::get_Length() L_0004 : callvirt例如int32 System.String : : get_Length ( )
L_0009: ldc.i4.0 L_0009 : ldc.i4.0
L_000a: ceq L_000a : ceq
L_000c: ret L_000c :漚
L_000d: ldc.i4.1 L_000d : ldc.i4.1
L_000e: ret L_000e :漚
} )
This translates to (C#):這轉化為( C #中) :
public static bool IsNullOrEmpty(string value)公共靜態布爾IsNullOrEmpty (字符串值)
{ (
if (value != null)如果(值! =無效)
{ (
return (value.Length == 0);返回( value.Length == 0 ) ;
} )
return true;回歸真實;
} )
Now while you can perform this operation on your own, the whole point of the .NET Framework is the fact that this is managed code.而現在您可以執行此操作,在您自己的,整個點。 NET框架的事實是,這是託管代碼。 If for some reason Microsoft decides to change the implementation of the string class such that there is another case in which it is empty, it would change its implementation here.如果由於某種原因,微軟決定改變執行字符串類等,還有另一種情況下,它是空的,它將改變其執行這裡。 And you would never have to worry about your code breaking due to an underlying change.你將永遠不必擔心你的代碼打破由於一個基本的變化。
So, in short, use string.IsNullOrEmpty().因此,在短,使用string.IsNullOrEmpty ( ) 。