I usually go with:[color=blue]
> MyString == null || MyString.Length == 0[/color]
Poking around a bit with Lutz Roeder's .Net Reflector, it seems that this
version is the equivalent of[color=blue]
> MyString == null || MyString.m_stri ngLength == 0[/color]
while the other version if the equivalent of
[color=blue]
> MyString == null ||[/color]
CultureInfo.Cur rentCulture.Com pareInfo.Compar e(MyString, new String (""),
CompareOptions. None) == 0
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=blue]
> which one do you use and why?
>
> MyString == null || MyString == ""
>
> vs
>
> MyString == null || MyString.Length == 0
>
>[/color]
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:[color=blue]
> which one do you use and why?
>
> MyString == null || MyString == ""
>
> vs
>
> MyString == null || MyString.Length == 0[/color]
Well, the latter is slightly more efficient, so you should use that if
this is a bottleneck in your application, but really it's a case of
whichever you find most readable, in most cases. I personally prefer
the first, but it's really a matter of taste.
It's a trade off between a slight advantage in clarity vs a slight advantage
in performance (actually MyString == "" has something more than a slight
performance advantage in theory, but in practice it isn't that often that it
amounts to so much of a difference that a user would actually notice).
I personally use MyString.Length == 0 out of habit, as I think it's pretty
self-explanatory.
As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it. It would also have the advantage of being utterly clear, except
that in my experience many developers seem fuzzy on exactly what Empty
really means. I mean, *I'm* clear on what it means yet when I first
encountered String.Empty I felt it necessary to check the docs to make sure
it meant what I thought it did. And most languages have an unambiguous way
to express an empty string as a constant similar to "", so I prefer "" as
clearer than String.Empty.
--Bob
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=blue]
> which one do you use and why?
>
> MyString == null || MyString == ""
>
> vs
>
> MyString == null || MyString.Length == 0
>[/color]
> As an aside, String.Empty is completely equivalent to MyString == "",[color=blue]
> performance-wise -- String.Empty is simply a language-independent way to
> express it[/color]
Developpez.com, le Club des Développeurs et IT Pro
"Bob Grommes" wrote:
[color=blue]
> It's a trade off between a slight advantage in clarity vs a slight advantage
> in performance (actually MyString == "" has something more than a slight
> performance advantage in theory, but in practice it isn't that often that it
> amounts to so much of a difference that a user would actually notice).
>
> I personally use MyString.Length == 0 out of habit, as I think it's pretty
> self-explanatory.
>
> As an aside, String.Empty is completely equivalent to MyString == "",
> performance-wise -- String.Empty is simply a language-independent way to
> express it. It would also have the advantage of being utterly clear, except
> that in my experience many developers seem fuzzy on exactly what Empty
> really means. I mean, *I'm* clear on what it means yet when I first
> encountered String.Empty I felt it necessary to check the docs to make sure
> it meant what I thought it did. And most languages have an unambiguous way
> to express an empty string as a constant similar to "", so I prefer "" as
> clearer than String.Empty.
>
> --Bob
>
> "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
> news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=green]
> > which one do you use and why?
> >
> > MyString == null || MyString == ""
> >
> > vs
> >
> > MyString == null || MyString.Length == 0
> >[/color]
>
>
>[/color]
I take his statement with a grain of salt. String.Empty is a string
instance that presumably already exists as he states (it's a static
instance), but "" not just "probably" but *wil*" come out of the string
intern pool, as do all string constants in programs. As even that blogger
admits, the difference would be insignificant in any case. And I've seen
more than one other confident statement that String.Empty and "" are exactly
identical. Personally I don't have the time to go through the Rotor source
to figure it out.
The truth is that these are the little rationalization s we create to
validate our favorite way of doing things. Value == "", Value ==
String.Empty, and String.Length == 0 are in fact all perfectly fine and in
almost all real-world scenarios even String.Length == 0 will not produce a
*significant* (user-noticeable) difference in performance.
Only we geeks can thoroughly debate a topic this unimportant to death ;-)
--Bob
"LEBRUN Thomas" <lebrun_thomas_ at_hotmail.com> wrote in message
news:02784ADA-3E2F-4D51-918E-91780B250DAF@mi crosoft.com...[color=blue][color=green]
>> As an aside, String.Empty is completely equivalent to MyString == "",
>> performance-wise -- String.Empty is simply a language-independent way to
>> express it[/color]
>
> Not exactly ;)
>
> The post of Brad Adams
> (http://blogs.msdn.com/brada/archive/.../22/49997.aspx) confirm that
> using "" create an object while String.Empty create no onject.
>
> So if you are really looking for ultimately in memory efficiency, use
> String.empty
>
> Bye.
>
> -------------------
> LEBRUN Thomas
> http://morpheus.developpez.com
> http://blog.developpez.com/index.php?blog=9
>
> "Bob Grommes" wrote:
>[color=green]
>> It's a trade off between a slight advantage in clarity vs a slight
>> advantage
>> in performance (actually MyString == "" has something more than a slight
>> performance advantage in theory, but in practice it isn't that often that
>> it
>> amounts to so much of a difference that a user would actually notice).
>>
>> I personally use MyString.Length == 0 out of habit, as I think it's
>> pretty
>> self-explanatory.
>>
>> As an aside, String.Empty is completely equivalent to MyString == "",
>> performance-wise -- String.Empty is simply a language-independent way to
>> express it. It would also have the advantage of being utterly clear,
>> except
>> that in my experience many developers seem fuzzy on exactly what Empty
>> really means. I mean, *I'm* clear on what it means yet when I first
>> encountered String.Empty I felt it necessary to check the docs to make
>> sure
>> it meant what I thought it did. And most languages have an unambiguous
>> way
>> to express an empty string as a constant similar to "", so I prefer "" as
>> clearer than String.Empty.
>>
>> --Bob
>>
>> "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
>> message
>> news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
>> > which one do you use and why?
>> >
>> > MyString == null || MyString == ""
>> >
>> > vs
>> >
>> > MyString == null || MyString.Length == 0
>> >[/color]
>>
>>
>>[/color][/color]
LEBRUN Thomas <lebrun_thomas_ at_hotmail.com> wrote:
[color=blue]
> So if you are really looking for ultimately in memory efficiency, use
> String.empty[/color]
FxCop tells me that testing String.Length against 0 is more efficient.
Micro-optimization, while interesting, is almost never a reason to write code in a particular way. Clarity and maintainability are hugely more important to me.
LEBRUN Thomas <lebrun_thomas_ at_hotmail.com> wrote:
[color=blue]
> So if you are really looking for ultimately in memory efficiency, use
> String.empty[/color]
FxCop tells me that testing String.Length against 0 is more efficient.
Developpez.com, le Club des Développeurs et IT Pro
"C# Learner" wrote:
[color=blue]
> LEBRUN Thomas <lebrun_thomas_ at_hotmail.com> wrote:
>[color=green]
> > So if you are really looking for ultimately in memory efficiency, use
> > String.empty[/color]
>
> FxCop tells me that testing String.Length against 0 is more efficient.
>[/color]
Comment