Home घर » » Dot Net डॉट नेट

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 / / सी # गलत तरीके

  1. if ( s == “” ) अगर (ओं == "")
  2. if ( s == string.Empty ) ओं == string.Empty (यदि)
  3. if ( s.Equals(””) ) अगर (s.Equals ( ""))
  4. if ( s.Equals ( String.Empty) अगर (s.Equals (String.Empty)
  5. if ( string.Equals(s,””) अगर (string.Equals (s, "")
  6. 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. पढ़ें नीचे सही दृष्टिकोण के लिए.

  • Ryan Heaney रियान Heaney , March 27, 2008: , 27 मार्च, 2008:

    I agree with Shahar. मैं शहर से सहमत हूँ. string.IsNullOrEmpty is the best, managed way to test. string.IsNullOrEmpty का सबसे अच्छा, परीक्षण करने के लिए तरीका कामयाब रहा है. It's static and guaranteed not to throw a NullReferenceException. यह स्थिर है और एक NullReferenceException फेंकने की गारंटी नहीं. You can't say that about .Length. तुम उस के बारे में नहीं कह सकता. लम्बाई.

    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 में ().

Tagged with: के साथ टैग की गईं:

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanΜεταφράστε στα ελληνικά/GreekПереведите к русскому/RussianOversetter til Norsk/NorwegianÖversätta till Svensk/Swedishहिन्दी अनुवाद करने के लिए/Hindi
Tradueix al català/CatalanTulkot uz latviešu/LatvianPreložiť do slovenčiny/SlovakVertaal aan het Nederlands/Dutchترجمة الى العربية/ArabicTraduzca al Español/SpanishTraduisez au Français/French
Traduca ad Italiano/ItalianTraduza ao Português/Portuguese日本語に翻訳しなさい /Japanese한국어에게 번역하십시오/Korean中文翻译/Chinese Simplified中文翻译/Chinese TraditionalПереклад на українську/Ukrainian

एसएमएस सदस्यताPrint This Post

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 सहायता केंद्र

Leave your response! आपकी प्रतिक्रिया छोड़ दो!

Be nice. अच्छे बनो. Keep it clean. यह साफ रखें. Stay on topic. रहो विषय पर. No spam. स्पैम नहीं.

You can use these tags: आप इन टैग का उपयोग कर सकते हैं:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>