Click here to Skip to main content
16,005,181 members
Home / Discussions / C#
   

C#

 
AnswerRe: or || operator on strings? Pin
Alaric_29-Nov-07 8:21
professionalAlaric_29-Nov-07 8:21 
GeneralRe: or || operator on strings? Pin
PIEBALDconsult29-Nov-07 8:31
mvePIEBALDconsult29-Nov-07 8:31 
GeneralRe: or || operator on strings? Pin
Alaric_29-Nov-07 8:45
professionalAlaric_29-Nov-07 8:45 
GeneralRe: or || operator on strings? Pin
h0st1le29-Nov-07 8:51
h0st1le29-Nov-07 8:51 
GeneralRe: or || operator on strings? Pin
PIEBALDconsult29-Nov-07 10:01
mvePIEBALDconsult29-Nov-07 10:01 
GeneralRe: or || operator on strings? Pin
Luc Pattyn29-Nov-07 11:14
sitebuilderLuc Pattyn29-Nov-07 11:14 
GeneralRe: or || operator on strings? Pin
Anthony Mushrow29-Nov-07 12:41
professionalAnthony Mushrow29-Nov-07 12:41 
GeneralRe: or || operator on strings? Pin
Luc Pattyn29-Nov-07 13:17
sitebuilderLuc Pattyn29-Nov-07 13:17 
Hi,

this is how I see it, after some experiments:

str=="abc" generates MSIL containing a call to the static method
String.op_Equality(string,string)

str.Equals("abc") generates MSIL containing a virtual call to the
instance method String.Equals(string)

when str is a valid (non-null) string reference, both methods are supposed to do
exactly the same.

But when str happens to be null, the first will succeed, whereas the second will
throw a NullReferenceException.

Given all this, I never plan on using the latter.

BTW: there also is the static method String.Compare with many overloads, yielding
more than just an equal/unequal result.

The C# compiler treats strings in special ways, to make life easier. To name a few:
- string literals (with double quotes)
- string comparison (the shorthand way)
- switching on a string.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]


this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets


GeneralRe: or || operator on strings? Pin
h0st1le29-Nov-07 8:32
h0st1le29-Nov-07 8:32 
GeneralRe: or || operator on strings? Pin
Ennis Ray Lynch, Jr.29-Nov-07 8:34
Ennis Ray Lynch, Jr.29-Nov-07 8:34 
GeneralRe: or || operator on strings? Pin
h0st1le29-Nov-07 8:46
h0st1le29-Nov-07 8:46 
GeneralRe: or || operator on strings? Pin
Luc Pattyn29-Nov-07 9:05
sitebuilderLuc Pattyn29-Nov-07 9:05 
GeneralRe: or || operator on strings? Pin
h0st1le29-Nov-07 9:09
h0st1le29-Nov-07 9:09 
GeneralRe: or || operator on strings? Pin
Alaric_30-Nov-07 3:03
professionalAlaric_30-Nov-07 3:03 
GeneralUse Pin
Ennis Ray Lynch, Jr.1-Dec-07 17:19
Ennis Ray Lynch, Jr.1-Dec-07 17:19 
AnswerRe: or || operator on strings? Pin
PIEBALDconsult29-Nov-07 8:36
mvePIEBALDconsult29-Nov-07 8:36 
GeneralRe: or || operator on strings? Pin
h0st1le29-Nov-07 8:50
h0st1le29-Nov-07 8:50 
GeneralRe: or || operator on strings? Pin
Alaric_29-Nov-07 9:01
professionalAlaric_29-Nov-07 9:01 
GeneralRe: or || operator on strings? Pin
h0st1le29-Nov-07 9:07
h0st1le29-Nov-07 9:07 
GeneralRe: or || operator on strings? Pin
PIEBALDconsult29-Nov-07 10:07
mvePIEBALDconsult29-Nov-07 10:07 
AnswerRe: or || operator on strings? Pin
Guffa29-Nov-07 13:46
Guffa29-Nov-07 13:46 
GeneralRe: or || operator on strings? Pin
Luc Pattyn29-Nov-07 14:35
sitebuilderLuc Pattyn29-Nov-07 14:35 
AnswerRe: or || operator on strings? Pin
PIEBALDconsult30-Nov-07 3:39
mvePIEBALDconsult30-Nov-07 3:39 
GeneralRe: or || operator on strings? [modified] Pin
Luc Pattyn30-Nov-07 5:56
sitebuilderLuc Pattyn30-Nov-07 5:56 
GeneralRe: or || operator on strings? [modified] Pin
PIEBALDconsult30-Nov-07 10:09
mvePIEBALDconsult30-Nov-07 10:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.