Karthik
Karthik

Reputation: 187

Why do we have to use ref for a type of String when it is basically a class?

Class is a reference types while Struct is a value type.

This means that I do not need to explicitly specify ref for every parameter which is a type of class.

My Question is,

Why do we have to specify a ref keyword for a string type which is basically a type of class ?

Please find the example below

public partial class StringManupulation : Form
{
    public StringManupulation()
    {
        InitializeComponent();

        String value = "Hello ";

        Concatenate( value );
        MessageBox.Show( value );

        Concatenate( ref value );
        MessageBox.Show( value );
    }

    /// <summary>
    /// Does not work
    /// </summary>
    /// <param name="value"></param>
    public void Concatenate( String value )
    {
        value = string.Concat( value, "StackOverflow" );
    }

    /// <summary>
    /// Works! 
    /// </summary>
    /// <param name="value"></param>
    public void Concatenate( ref String value )
    {
        value = string.Concat( value, "StackOverflow" );
    }
 }

Upvotes: 0

Views: 288

Answers (4)

KV Prajapati
KV Prajapati

Reputation: 94653

System.String is immutable - That means you cannot change the content of string once it is assigned. You may try System.Text.StringBuilder (mutable object) instead of System.String class.

static void ParamTest(System.Text.StringBuilder  paramSb)
{
  paramSb.Append("World");
}

static void Main()
{
  System.Text.StringBuilder sb = new StringBuilder();
  sb.Append("Hello");

  ParamTest(sb);
  Console.WriteLine(sb);
}

Have a look at Strings in C# and .NET and article - chapter from C# in Depth.

Quote to Jon Skeet

It is a reference type?

It's a common misconception that string is a value type. That's because its immutability (see next point) makes it act sort of like a value type. It actually acts like a normal reference type. See my articles on parameter passing and memory for more details of the differences between value types and reference types.

Upvotes: 3

Nop
Nop

Reputation: 151

You have to use the ref keyword when you assign new reference to a variable.

class A {
    public string prop { get; set; }
}

public void test(A a)
{
    a = new A(); // NO, it's a new ref
    a.prop = "foo"; // Yes, you modify the object it's not a new ref.
}

public void test2(string s)
{
    s = "bar"; // Equivalent to s = new String("bar"), so it's a new ref.
}

Upvotes: 0

bobbymcr
bobbymcr

Reputation: 24167

Two things to be aware of:

  • Strings are immutable in .NET. You cannot through any normal means alter the contents of a string instance.
  • Reassigning a reference in a method requires ref. If you want to change the underlying reference inside a method and have this change be visible to the caller, you must pass it by ref.

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108840

That's not specific to string, that's the case of any parameter. If you want to change the variable passed in a parameter, you need to pass it by reference.

When passing by value, you can only change the contents of the passed in instance of a reference type. And string has no mutable content.

Upvotes: 6

Related Questions