Click here to Skip to main content
15,891,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have in the project the next line.

rdr = scomm.ExecuteReader();
log.Debug("log 1" + rdr.Read() + " " + rdr.GetString(0) + " " + String.ReferenceEquals(rdr.GetString(0), "SUCCES"));

and this is return in the logfile log 1True SUCCES False.

I do not understand why "String.ReferenceEquals(rdr.GetString(0), "SUCCES")" is not true.

Thank you in advance!

What I have tried:

I tried to verify the result of the sql command execution.
Posted
Comments
Andreas Gieriet 17-Feb-16 4:29am    
Have you checked the MSDN documentation of ReferenceEquals?
Cheers
Andi

 
Share this answer
 
Have also a look at this CodeProject article: Comparing Values for Equality in .NET: Identity and Equivalence[^].
 
Share this answer
 
Don't do things like that!
The problem is that you don't control the execution order: the compiler (and worse the optimiser) is at liberty to evaluate the "elements" of that string in any order it chooses - and it doesn't know that the Read method needs to be called first.
Try explicitly ordering the operations:
C#
rdr = scomm.ExecuteReader();
bool isData = rdr.Read();
string s1 = rdr.GetString(0);
string s2 = String.ReferenceEquals(rdr.GetString(0), "SUCCES");
log.Debug("log 1" + isData + " " + s1 + " " + s2);

As to why they aren't the same reference, the intern pool for strings is only applied to string literals - not to strings that aren't defined at compile time. Try this:
C#
        private string GC(string s1, string s2)
            {
            return s1 + s2;
            }
...
            string s1 = GC("hello", " there");
            string s2 = GC("hello", " there");
            string s3 = "hello there";
            string s4 = "hello there";

            Console.WriteLine("{0}:{1}", s1 == s2, string.ReferenceEquals(s1, s2));
            Console.WriteLine("{0}:{1}", s1 == s3, string.ReferenceEquals(s1, s3));
            Console.WriteLine("{0}:{1}", s3 == s4, string.ReferenceEquals(s3, s4));
And you will get:
True:False
True:False
True:True
 
Share this answer
 
v2
C#
rdr.GetString(0).Equals("SUCCES")


Also "success" is spelled like that, so make sure you don't have a typo either.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900