In this article we’ll discuss whether string is reference type or value type. Most of the time this question is asked in interview. String is reference type but always behave as value type. I have following sample code in which I am assigning some text to “name” variable and then will try to change it using “nameChange(name)” function.

public static void Main(string[] args)
  {
            string name = "John Smith";
            nameChange(name);
            Console.WriteLine(name);
            Console.Read();
 }

Now body of above method  will be like

  static void nameChange(String name)
  {
            name = "Rakesh Kumar";
 }

Please hold on and think, what will be output.

Does the name will change form “John Smith” to “Rakesh Kumar”. If you are thinking ‘YES’ then you are wrong. Answer is ‘NO’, name will not change.

Output will be as:

Why this happened: 

Although string is reference type but always behave as value type so new copy will be created inside the function and but it will not affect the actual copy created in the beginning.

Is there no way by which we can change the value, of course YES, we can use ref keyword. 

Decorate your method’s parameter with out or ref keyword as

            string name = "John Smith";
            nameChange(ref name);
            Console.WriteLine(name);
            Console.Read();

//Now body of above method  will be

        static void nameChange(ref String name)
        {
            name = "Rakesh Kumar";
         }

Now output will be as:

In next example we’ll discuss about Reference type:

     I am taking an array of string type. I assign three values in it as you can see in below example

string[] arr1 = new string[] { "One", "Two", "Three","Four"};
            ArrayChange(arr1);
            Console.WriteLine(String.Format("{0},{1},{2},{3}", arr1[0], arr1[1], arr1[2],arr1[3]));
            Console.Read();
/*
Now “ArrayChange” method will have following code in it. I'll change the value which is at index [1].
Does it will change? YES, it will change the value.
*/
        static void ArrayChange(string[] arr1)
        {
            arr1[1] = "Rakesh";
        }

This is simple demonstration for your understanding. For further deep understanding we will see more in next article that how the addresses/values are behaving. For more information on pointers in C# you can refer link HERE.

For more details of reference type vs value type and pointes in C# you can check out HERE

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can