Is String reference type or Value type in C#?

If we go to definition of string class in Visual Studio it represent string as sealed class (reference type) as per below screen shot: ![alt text][1] Then after If I tested the following code then it behaves like value type. static void Main(string[] args) { string name = "test"; ChangeName(name); Console.WriteLine(name); // this will still refer to test not test2 so behaves like ValueType Console.ReadKey(); } public static void ChangeName(string name) { name = "test2"; } We can also assign null value to string. So in this way string behaves like reference type. So I am enthusiastic to know the reason why string behaves sometime as ValueType and Sometimes as Reference Type [1]:
http://techoverflow.int.thomsonreuters.com/upfiles/Capture_5.PNG
Tagged:

Answers

  • string is an alias for System.String. Therefore your 'name' variable is a reference to a String object. [References are passed by value by default][1]. This sort of question is not particular to TR technologies. From the [FAQ][2]: > **What kinds of questions can I ask > here?** > > Before you ask, please make sure to > search for a similar question. You can > search for questions by their title, > content, or tags. Ask tech questions > about projects, systems, or techniques > used by Thomson Reuters professional > technology. > > Add tags to your question to keep them > organized relative to technologies > from other departments. > > **What kinds of questions should be > avoided?** > > Avoid asking questions that are not > relevant to this community, are too > subjective or argumentative. Avoid > asking questions about industry > technologies that are not specific to > Thomson Reuters technology. Those > general industry questions and answers > can be fielded using Google, > StackOverflow, or other similar > external resources. [1]:
    http://msdn.microsoft.com/en-us/library/0f66670z.aspx [2]:
    http://techoverflow.int.thomsonreuters.com/faq/