Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi Everyone,

I am a newbie to C#.Can anyone tell me whether string is a value type or reference type in C#.net.Some sources say ..it is a value type, some say it is Refernce type and some say it is a reference type but acts like a value type..so confused..Kinldy help me how "String" behaves in C#?


regards
Chaithanya M
Posted

Not sure if this has been posted yet, but here is the definition from the MSDN site:

http://msdn.microsoft.com/en-us/library/362314fe%28VS.71%29.aspx[^]

You're right, the string type is confusing (because it seems to be both a reference AND a value type). But this is how MS is defining it.

Dre---
 
Share this answer
 
Check MSDN: string (C# reference)[^]

The second line states:
Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive.

So it is a reference type but with some value-type behaviour ;)
 
Share this answer
 
Comments
TweakBird 13-Apr-11 13:22pm    
MSDN is always great reference. My 5.
Olivier Levrey 13-Apr-11 15:16pm    
Thank you E$w@r
TweakBird 13-Apr-11 15:19pm    
Welcome
This is a reference type but simulating value semantic in all aspects. For example, when you compare two referentially different strings, comparison (==) can be true — they are compared by the content. However, this is easy part: you can implement your own identity rules overriding System.Object.Equals, System.Object.GetHashCode (required) and optionally define custom operators "==" and "!=".

You can understand the essence of working with string references if you look at the properties system.String.Intern and system.String.IsInterned:
http://msdn.microsoft.com/en-us/library/system.string.intern.aspx[^],
http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx[^].

One important practical issue with the strings: they are immutable (in contrast to mutable arrays). Therefore, for performance reasons one should never use "+" in cycles. For this (and many other) purposes, use System.Text.StringBuilder.

—SA
 
Share this answer
 
v3
Comments
#realJSOP 13-Apr-11 11:29am    
Looks like a new semester has started in near-east universities...
Sergey Alexandrovich Kryukov 13-Apr-11 11:31am    
He he. Where is my professors salary?
--SA
Olivier Levrey 13-Apr-11 11:45am    
Good info. My 5. I knew string was a strange animal in .NET world, but I didn't know the details.
Sergey Alexandrovich Kryukov 13-Apr-11 11:57am    
Thank you, Olivier.
Good thing about it: you don't have to know the cunning detail to use them. It's good to know they are immutable and never use "+" in cycle though :-)
--SA
TweakBird 13-Apr-11 13:21pm    
Good Answer. my 5.
I think in the context you are asking it is a value type. The code below will explain better than my sentences I think...

void Test(){
   string s = "A";//set string to A
   Change(s);
   //if s was reference type
   //   s would = "B"
   //if a was value type
   //   s would = "A" still

   //s will = "A" hence it is value type
}

void Change(string s){
   s = B";
}
 
Share this answer
 
Comments
Olivier Levrey 13-Apr-11 11:47am    
You are right, in this case string behaves like a value type, although it is not. My 5 anyway.
Take a look here Strings[^]
 
Share this answer
 
Comments
RaviRanjanKr 13-Apr-11 11:37am    
Good Link! My 5
walterhevedeich 13-Apr-11 12:36pm    
Thanks
Olivier Levrey 13-Apr-11 11:48am    
Interesting link. My 5.
walterhevedeich 13-Apr-11 12:37pm    
Thanks.
TweakBird 13-Apr-11 13:20pm    
Good Link. My 5

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