-1

A class can set name from a method and it keep value: example

Employeer e  = new Employeer()
SetName(e);

But we don't need to add ref when call method SetName(e) But a string you need to add ref

string name = "";
SetName(ref name);

who can explain detail about it? thanks

Hong Van Vit
  • 2,884
  • 3
  • 18
  • 43

3 Answers3

3

This is the difference between changing a variable's value and mutating a reference type object's state.

With Employee, you are doing the latter. With string, you are doing the former.

With reference types, the references to the objects are copied and passed around. This is why you can change the name of the employee just by passing e. However, what if you reassigned e inside the method?

// in the method
e = null;

Will the e passed in become null? No. This is because you are making the copied reference point to something else. It has nothing to do with the original reference that you have when you created the employee.

This is the same thing with string, since string is also a reference type. When you change the passed in string to something else inside the method:

myString = "hello";

The change will not reflect outside the method. In theory, you can mutate he string's state inside a method and it will reflect outside the method. However, string is immutable (does not expose anything that lets you change its state).

When you add ref to it, it becomes possible to do this because it kind of passes a reference of the reference that refers to your objects. Therefore, you are able to reassign stuff inside the method and the changes will be reflected outside.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

In C# string datatype has the following properties: • String is a reference type but behaves like a value type. • Strings are immutable i.e., every time you change a string then a new string is created and the reference variable changes its reference to the newly created string.

A reference variable to an object type is always passed by reference (might seem tricky but you will get it if you continue reading :)). But in case of string the reference variable passed by default is passed by value (unless you have explicitly use the ref keyword). So in the following case:

String hello = "Hello";
MyFunction(hello);

A new reference variable will be created in MyFucntion that refers to the same location as hello string. But when you change the hello function inside the MyFucntion then actually contents of the newly created reference variable will be changed.

void MyFuntion(String hello)
{
    //a new string will be created and the reference value of the 
    //local hello will be changed
    hello = "Bye";
}

Let’s get into more details with the help of another example: Consider the following code:

Inside main:

//let's create a string and pass it to a function as value and reference type
String str = "Hello World";
StringRefernecePassedByValue(str, ref str);
//the reference that was passed by value is the one that is the actual reference
//other one was a new refernece variable that has a copy of the reference
Console.WriteLine("Actual String: " + str);

The function called:

//The body of the function
static void StringRefernecePassedByValue(String valStr,ref String refStr)
{
    //Both of the passed reference variable are pointing to the same location
    Console.WriteLine("Are equal?: " + ReferenceEquals(valStr, refStr));

    //let's change the strings
    valStr = "Hello1";//a new string is created and now the valStr refer to it
    refStr = "Hello2";//a new string is created and now the refStr refer to it

    //let's confirm the output
    Console.WriteLine("Passed by Value: " + valStr);
    Console.WriteLine("Passed by Reference: " + refStr);

    //now both are changed are are no more refering to the same location
    Console.WriteLine("Are equal?: " + ReferenceEquals(valStr, refStr));
}

The output is:

Are equal?: True
Passed by Value: Hello1
Passed by Reference: Hello2
Are equal?: False
Actual String: Hello2

Now let’s do the above for a user defined type:

//a user defined datatype
class Student
{
    public String name { get; set; }
    public String cgpa { get; set; }
}

Inside main:

//let's observe in case of Custom type
Student student = new Student();
student.name = "Ali";
student.cgpa = "3.5";

ClassRefernecePassedByValue(student, ref student);
Console.WriteLine("Actual String: " + student.name);

The function to be called:

//the body of function
static void ClassRefernecePassedByValue(Student valStd, ref Student refStd)
{
    //Both of the passed reference variable are pointing to the same location
    Console.WriteLine("Are equal?: " + ReferenceEquals(valStd, refStd));

    //let's change the objects
    valStd.name = "Joseph";//changing the object refered by valStd
    refStd.name = "Mick";//changing the object refered by refStd 

    Console.WriteLine("Passed by Value: " + valStd.name);
    Console.WriteLine("Passed by Reference: " + refStd.name);

    //now both are same
    Console.WriteLine("Are equal?: " + ReferenceEquals(valStd, refStd));
}

The output:

Are equal?: True
Passed by Value: Mick
Passed by Reference: Mick
Are equal?: True
Actual String: Mick

Thus concluding that the reference to a string when passed to a function actually creates a new reference variable which contains a copied reference to another string reference variable but as string is an immutable type thus when the string pointed by the new reference variable changes then actual reference variable stays unchanged. enter image description here

Also for further understanding you can get the complete code here

Tayyab
  • 1,207
  • 8
  • 29
0

if you don't pass string with ref keyword during method call then it create an new instance of string and changes are made on new instance of string .after returning on the method the string value which we pass does not affected