C#: How to compare variables by value and reference type
-
i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code
int x = 1000;
int y = 1000;if (x == y) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (x.Equals(y)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(x,y)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same"); string s1 = "Hello"; string s2 = "Hello"; if (s1 == s2) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (s1.Equals(s2)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(s1, s2)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same");
i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?
if (object.ReferenceEquals(x,y))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check
if (object.ReferenceEquals(s1, s2))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks
tbhattacharjee
-
i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code
int x = 1000;
int y = 1000;if (x == y) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (x.Equals(y)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(x,y)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same"); string s1 = "Hello"; string s2 = "Hello"; if (s1 == s2) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (s1.Equals(s2)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(s1, s2)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same");
i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?
if (object.ReferenceEquals(x,y))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check
if (object.ReferenceEquals(s1, s2))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks
tbhattacharjee
Do not feed the help vampire[^]
This space for rent
-
i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code
int x = 1000;
int y = 1000;if (x == y) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (x.Equals(y)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(x,y)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same"); string s1 = "Hello"; string s2 = "Hello"; if (s1 == s2) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (s1.Equals(s2)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(s1, s2)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same");
i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?
if (object.ReferenceEquals(x,y))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check
if (object.ReferenceEquals(s1, s2))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks
tbhattacharjee
-
i have a small confusion that when we compare like == or equal function then both does the same job. u have a very basic question here. see my program full code
int x = 1000;
int y = 1000;if (x == y) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (x.Equals(y)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(x,y)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same"); string s1 = "Hello"; string s2 = "Hello"; if (s1 == s2) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (s1.Equals(s2)) Console.WriteLine("Value is same"); else Console.WriteLine("Value is not same"); if (object.ReferenceEquals(s1, s2)) Console.WriteLine("ref is same"); else Console.WriteLine("ref is not same");
i know that this kind of checking if (x == y) is based on value but when i use Equals function then i saw Equals is also work like == operator....am i right ? how to check the reference ?
if (object.ReferenceEquals(x,y))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");i saw in this case else portion execute........why because different memory is allocated for x and y ? see more for string reference check
if (object.ReferenceEquals(s1, s2))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");in this scenario s1 and s2 ref found same which is not clear to me because s1 and s2 ref should be different because two are different variable so how s1 and s2 reference check become same? i like to know what are the best process to know value and reference is same or not whatever data type we use may be string or integer or float etc. please some one help me to understand this. one guy said string in dotnet is interned. what is the meaning of In .NET strings are interned ? interned or internal is same ? please discuss with a example just to clarify the meaning of string is interned in dotnet. thanks
tbhattacharjee
// note: x and y are instances of value types
if (object.ReferenceEquals(x, y))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");In this context that will always be not the same, even
object.ReferenceEquals(x, x)
.object.ReferenceEquals
takesobject
s as parameters, so if you pass in a struct (including most primitive types) it will have to be boxed, giving you a new box. Every time. No two new boxes are ever the same, because they're new. If you had boxed them before and then pass the same box twice, of course the same box is the same as the same box. That sounds more confusing than it is.Tridip Bhattacharjee wrote:
how to check the reference ?
You don't in this case. There is no reference, you just have a couple of
int
s there. You can compare the references to boxed versions of ints, as you did, but it's not very useful. String is a reference type though, so there's no boxing involved. Different strings, of course, will definitely have different references. Equal strings may also have different references, depending on how they were created. The default == and Equals for reference types is reference equality, butstring
overrides Equals and implements == to mean value-equality (unlike Java, where String does override Equals, but cannot implement == because that's not a thing you can do in Java). This can come together to cause some fun when looking at both reference equality (by casting toobject
or usingobject``.ReferenceEquals
) and the usual value-equality for some strings.Tridip Bhattacharjee wrote:
s1 and s2 ref should be different because two are different variable
Well, that's never how reference equality works. You can have the same reference in however many places you want, for example in
string s1 = "Hello"; string s2 = s1;
is it clear that s1 and s2 not only refer to strings with the same contents, but the same string object, and that would be the case for any reference type (but not value types, for which reference equality is useless to even talk about). Possibly you hav
-
// note: x and y are instances of value types
if (object.ReferenceEquals(x, y))
Console.WriteLine("ref is same");
else
Console.WriteLine("ref is not same");In this context that will always be not the same, even
object.ReferenceEquals(x, x)
.object.ReferenceEquals
takesobject
s as parameters, so if you pass in a struct (including most primitive types) it will have to be boxed, giving you a new box. Every time. No two new boxes are ever the same, because they're new. If you had boxed them before and then pass the same box twice, of course the same box is the same as the same box. That sounds more confusing than it is.Tridip Bhattacharjee wrote:
how to check the reference ?
You don't in this case. There is no reference, you just have a couple of
int
s there. You can compare the references to boxed versions of ints, as you did, but it's not very useful. String is a reference type though, so there's no boxing involved. Different strings, of course, will definitely have different references. Equal strings may also have different references, depending on how they were created. The default == and Equals for reference types is reference equality, butstring
overrides Equals and implements == to mean value-equality (unlike Java, where String does override Equals, but cannot implement == because that's not a thing you can do in Java). This can come together to cause some fun when looking at both reference equality (by casting toobject
or usingobject``.ReferenceEquals
) and the usual value-equality for some strings.Tridip Bhattacharjee wrote:
s1 and s2 ref should be different because two are different variable
Well, that's never how reference equality works. You can have the same reference in however many places you want, for example in
string s1 = "Hello"; string s2 = s1;
is it clear that s1 and s2 not only refer to strings with the same contents, but the same string object, and that would be the case for any reference type (but not value types, for which reference equality is useless to even talk about). Possibly you hav
You're encouraging a help vampire. This user has been told, many times, that he should be able to do basic research for himself. If you look at his message history, you will see dozens of questions like this in this forum.
This space for rent
-
Do not feed the help vampire[^]
This space for rent
What gets me is that he's been here for 13 years, and he still knows nothing about development, and can't use Google ... :sigh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
What gets me is that he's been here for 13 years, and he still knows nothing about development, and can't use Google ... :sigh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!