Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
a simple program

C#
string s1 = "Faizan";
           string s2 = "Faizan";
           if (s1 == s2)
           {
               Console.WriteLine("yes");
           }
           else {
               Console.WriteLine("no");
           }
           Console.ReadLine();


surprisingly it is show yes to me. why string are object type the why it is equal?
Posted

In C#, each distinct string value is stored and is immutable.
Both variables point to the same string in memory and thus you get an equal result.

More detail available here[^].
 
Share this answer
 
v2
Comments
Kuthuparakkal 21-Feb-15 11:34am    
Well said +5
Abhinav S 21-Feb-15 12:08pm    
Thanks.
As you may know, in .NET you can't say that "everything inherits from Object" [^]: Interfaces, for example, do not inherit from Object.

The two fundamental flavors of Types that inherit from Object in .NET are System.ValueType , and System.ReferenceType ... then there are the Integral Types, such as Int32, which are a sub-species of ValueType (Integral Types are Structs, Structs inherit from ValueType inherit from Object).

While it's common to speak of all ValueTypes being stored on the Stack, and ReferenceTypes being stored on the Heap, the reality is not so clear-cut; I recommend you read these two articles by Eric Lippert about the Stack as "implementation detail: [^], [^].

Type String is a special case of ReferenceType: a String is an Immutable ReferenceType; it can't be changed once it is created [<a href="http://3.bp.blogspot.com/_gez10dNhuPk/Rb4IV-mHxVI/AAAAAAAAADk/viWcrNAGkCA/s400/3+Immutable+Reference+Types.gif" target="_blank" title="New Window">^</a>].

For Type String, the = and != operators are overloaded so they are compared using the Value (characters) in their content, rather than being compared by what they reference (the memory address they "point-to"). The C# language design of how Strings are implemented is (I have read) like that in Java.

So you can speak of Type String as a Reference Type, as Immutable, and having "value semantics:" the way you use it is like the way you use Value Types.

The reasons for the way Strings are defined in .NET are simple: efficiency, speed, optimizing memory requirements. The Immutability of Strings means that if you re-use the same String Literal many times in your Application, the Compiler will not duplicate the String data-contents, but only store its values (byte array) in memory once.

Immutability means that every time you perform an operation on a String that changes its internal value a copy of the String is made; this is why String methods like Trim, SubString return a copy rather than modify the String they are called on.
 
Share this answer
 
v2

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