-1

as title said "does sending an instance from a class has same effect as send it by ref"

for example

FillStudentInformation(student); 

has the same effect of

FillStudentInformation(ref student); 

Does I expect that both instance will be changed by this call in same manner.

Note: FillStudentUnformation is a void method

6
  • 1
    It depends of methods implementation. I think you should read this: msdn.microsoft.com/en-us/library/14akc2c7.aspx Commented Dec 24, 2014 at 7:29
  • No. You can only modify student properties with the first call while to can reassign student to another student instance with the second call Commented Dec 24, 2014 at 7:30
  • you should read this article: albahari.com/valuevsreftypes.aspx Commented Dec 24, 2014 at 7:31
  • See What is the use of “ref” for reference-type variables in C#?. Commented Dec 24, 2014 at 7:52
  • @JeppeStigNielsen I know what is reference but I am asking about the effect of sending an instance of class direct without ref Commented Dec 24, 2014 at 8:17

2 Answers 2

1

The main difference is that if you do something like inside the method:

student = new Student(); 

In the second case, you will change the student object outside of the method too (not in the first case).

Using things like:

student.Name = 'John Doe'; 

will work the same for both.

However, you should try to avoid reference as much as you can because it can lead to more side effects and a lower testability.

Sign up to request clarification or add additional context in comments.

Comments

0

You should not expect same behavior as both are different thing if I assume that Student is object of class(reference type). It also depend on that what you do inside method.

In First method

void Main() { Student student = new Student(); FillStudentUnformation(student); Console.WriteLine(student.Name); // Here you will not get name FillStudentUnformationRef(ref student); Console.WriteLine(student.Name); // you will see name you have set inside method. } vodi FillStudentUnformation(Student student) { //If here if you do student = new Student(); student.Name = "test"; // This will not reflect outside. } vodi FillStudentUnformationRef(ref Student student) { //If here if you do student = new Student(); student.Name = "test ref"; // This will not reflect outside. } 

So when we pass reference type to method it will pass copy of reference to that variable. So when you update that variable with new it will update that variable reference and not the actual one.

In second method it will pass actual reference so when you update it will affect the actual referene of object.

more on this : http://msdn.microsoft.com/en-IN/library/s6938f28.aspx

2 Comments

why in the first I will not get the name?I need to understand please
you are right but I need to understand why? as I thought that in both you send the current address we are in

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.