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. وهو واحد من الناس أن الخطأ مشترك مقارنة مع سلسلة "" أو String.Empty في VB.Net أو جيم # ليجد فارغة. 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 (ق ، "")
  6. 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 / / [جيم #] الطريقة الصحيحة

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 سترون ما تفعله.
    .method public hidebysig static bool IsNullOrEmpty(string ‘value’) cil managed . الطريقة العامة hidebysig ساكنة bool IsNullOrEmpty (سلسلة 'قيمة') تمكن cil
    { (
    .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#): وهذا يترجم إلى (ج #) :
    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. الآن بينما يمكنك إجراء هذه العملية بنفسك ، فإن كل نقطة من صافي الإطار هو حقيقة أن هذه هي تمكنت من مدونة. 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 ().

Tags: العلامات : , ،

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanПереведите к русскому/RussianΜεταφράστε στα ελληνικά/GreekVertaal aan het Nederlands/Dutchترجمة الى العربية/Arabic中文翻译/Chinese Traditional中文翻译/Chinese Simplified한국어에게 번역하십시오/Korean日本語に翻訳しなさい /JapaneseTraduza ao Português/PortugueseTraduca ad Italiano/ItalianTraduisez au Français/FrenchTraduzca al Español/Spanish
الاشتراك في الرسائل القصيرةPrint This Post

Posted on 6th April 2008 by موقع على 6th نيسان / أبريل 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 فيسبوك | | 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>