Click here to Skip to main content
15,902,636 members
Home / Discussions / C#
   

C#

 
AnswerRe: c# Introduction Pin
Richard MacCutchan10-Jan-18 22:28
mveRichard MacCutchan10-Jan-18 22:28 
GeneralRe: c# Introduction Pin
ormonds10-Jan-18 23:14
ormonds10-Jan-18 23:14 
QuestionHow to compare string data by value and by reference Pin
Mou_kol8-Jan-18 20:59
Mou_kol8-Jan-18 20:59 
AnswerRe: How to compare string data by value and by reference Pin
OriginalGriff8-Jan-18 21:24
mveOriginalGriff8-Jan-18 21:24 
AnswerRe: How to compare string data by value and by reference Pin
Bernhard Hiller9-Jan-18 21:41
Bernhard Hiller9-Jan-18 21:41 
GeneralRe: How to compare string data by value and by reference Pin
OriginalGriff10-Jan-18 0:38
mveOriginalGriff10-Jan-18 0:38 
GeneralRe: How to compare string data by value and by reference Pin
Mou_kol10-Jan-18 21:18
Mou_kol10-Jan-18 21:18 
GeneralRe: How to compare string data by value and by reference Pin
OriginalGriff10-Jan-18 22:02
mveOriginalGriff10-Jan-18 22:02 
At this stage, I'd say "don't worry about it" - because it starts to get pretty complicated. When you write code like that, two additional factors come into play: optimisation and interning.

Optimisation is not normally applied to debug builds - they are optimised for "debuggability" not performance and that means that large sections of the compiler do not get used that can make significant changes to your code. For example, the compiler can look at this:
C#
public bool IsSame()
   {
   string a = "Hello World";
   string b = "Hello World";
   return a == b;
   }
And "know" that it will always produce the same result: true. So the compiler can "throw away" the whole method and replace it with inline code for "true" wherever your code references it.
That makes it very difficult to say "it's this type of comparison" because the comparison may never occur at all, depending on what happens in the surrounding code! It doesn't do that for debug builds, because it makes it a lot harder for you the developer to work with in the debugger - you can't set a breakpoint on code which has been optimised out! Laugh | :laugh:

Interning is another complexity, and one I was hoping to avoid - and why I wrote my original code exactly the way I did. Interning basically means that because strings are immutable, the compiler "knows" that the two string literals are the same, so it only ever keeps one copy of it. So the comparison changes: it becomes a comparison between two identical references, so the question of "value or reference comparison?" becomes moot: they are the same. To see what I mean, try modifying my original code:
C#
string a = "Hello World";
string b = "Hello";
string c = " World";
string d = b + c;
string e = "Hello World";
Console.WriteLine("{0}:{1}:{2}:{3}", a == d, a.Equals(d), (object)a == (object)d, object.ReferenceEquals(a, d));
Console.WriteLine("{0}:{1}:{2}:{3}", a == e, a.Equals(e), (object)a == (object)e, object.ReferenceEquals(a, e));
Run that, and you will get different results for the two WriteLines:
True:True:False:False
True:True:True:True
The strings have been interned, so the references are the same.
If you want to know more - and this stuff can get quite complicated - I'd point you are the same link I did Bernhard: Understanding C#: String.Intern makes strings interesting - O'Reilly Broadcast[^] - but to be honest, this isn't stuff you need to know 99% of the time as it makes absolutely no different to how you code your app!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: How to compare string data by value and by reference Pin
Mou_kol11-Jan-18 3:42
Mou_kol11-Jan-18 3:42 
GeneralRe: How to compare string data by value and by reference Pin
Richard Deeming11-Jan-18 4:05
mveRichard Deeming11-Jan-18 4:05 
GeneralRe: How to compare string data by value and by reference Pin
Mou_kol15-Jan-18 22:00
Mou_kol15-Jan-18 22:00 
GeneralRe: How to compare string data by value and by reference Pin
Richard Deeming16-Jan-18 1:21
mveRichard Deeming16-Jan-18 1:21 
GeneralRe: How to compare string data by value and by reference Pin
Mou_kol16-Jan-18 1:44
Mou_kol16-Jan-18 1:44 
GeneralRe: How to compare string data by value and by reference Pin
OriginalGriff11-Jan-18 4:19
mveOriginalGriff11-Jan-18 4:19 
GeneralRe: How to compare string data by value and by reference Pin
Mycroft Holmes11-Jan-18 10:32
professionalMycroft Holmes11-Jan-18 10:32 
GeneralRe: How to compare string data by value and by reference Pin
OriginalGriff11-Jan-18 11:19
mveOriginalGriff11-Jan-18 11:19 
QuestionC# Windows Form Application with 86x unmanaged dll Pin
Zeyad Jalil8-Jan-18 20:33
professionalZeyad Jalil8-Jan-18 20:33 
AnswerRe: C# Windows Form Application with 86x unmanaged dll Pin
OriginalGriff8-Jan-18 21:16
mveOriginalGriff8-Jan-18 21:16 
AnswerRe: C# Windows Form Application with 86x unmanaged dll Pin
Gerry Schmitz10-Jan-18 8:23
mveGerry Schmitz10-Jan-18 8:23 
QuestionGame Pin
Member 136022178-Jan-18 10:55
Member 136022178-Jan-18 10:55 
AnswerRe: Game Pin
Pete O'Hanlon8-Jan-18 11:09
mvePete O'Hanlon8-Jan-18 11:09 
GeneralRe: Game Pin
Dave Kreskowiak8-Jan-18 11:26
mveDave Kreskowiak8-Jan-18 11:26 
AnswerRe: Game Pin
Swinkaran8-Jan-18 11:38
professionalSwinkaran8-Jan-18 11:38 
GeneralRe: Game Pin
PIEBALDconsult8-Jan-18 13:15
mvePIEBALDconsult8-Jan-18 13:15 
AnswerRe: Game Pin
Chris Quinn8-Jan-18 21:26
Chris Quinn8-Jan-18 21:26 

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.