5 Wrong ways to check empty strings खाली तार की जाँच करने के लिए 5 गलत तरीके
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. यह एक लोगों VB.Net या सी में "" के साथ या String.Empty # अपनी खाली खोजने के लिए एक स्ट्रिंग है कि तुलना में आम गलती की है. Here are few examples. ये कुछ उदाहरण हैं.
// C# Wrong Ways / / सी # गलत तरीके
- 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 (s, "")
- if ( string.Equals ( s,String.Empty )) अगर (string.Equals (, String.Empty) s)
So what's the correct way to do it ? तो क्या यह करने के लिए सही रास्ता है? Check for length too. चेक लंबाई के लिए भी.
// [ C# ] Correct Way / / [सी #] सही रास्ता
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# चेक लंबाई के लिए भी जब तुम VB.Net और सी में अशक्त तार की जाँच #
Read below for the right approach. पढ़ें नीचे सही दृष्टिकोण के लिए.
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |

Posted on 6th April 2008 by 6 अप्रैल 2008 पर द्वारा पोस्ट Ashish Mohta आशीष 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 आशीष Mohta | Connect with me @ | कनेक्ट मेरे साथ @ Twitter चहचहाना | | Linkedin Linkedin | | Facebook 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 के disassembly को देखो.
.method public hidebysig static bool IsNullOrEmpty(string 'value') cil managed . विधि सार्वजनिक hidebysig स्थिर bool IsNullOrEmpty (स्ट्रिंग 'मूल्य') सीआईएल प्रबंधन
{ (
.maxstack 8 . maxstack 8
L_0000: ldarg.0 L_0000: ldarg.0
L_0001: brfalse.s L_000d L_0001: L_000d brfalse.s
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#): यह (सी करने के लिए अनुवाद #):
public static bool IsNullOrEmpty(string value) सार्वजनिक स्थैतिक bool 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 Framework की सारी बात यह है कि कोड में कामयाब होता है यह इस तथ्य है. 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 में ().