-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

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Miral
  • 79
  • 11
  • 1
    It depends of methods implementation. I think you should read this: http://msdn.microsoft.com/en-us/library/14akc2c7.aspx – Vlad Dec 24 '14 at 07: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 – Tien Dinh Dec 24 '14 at 07:30
  • you should read this article: http://www.albahari.com/valuevsreftypes.aspx – MUG4N Dec 24 '14 at 07:31
  • See [What is the use of “ref” for reference-type variables in C#?](http://stackoverflow.com/questions/961717/). – Jeppe Stig Nielsen Dec 24 '14 at 07:52
  • @JeppeStigNielsen I know what is reference but I am asking about the effect of sending an instance of class direct without ref – Miral Dec 24 '14 at 08:17
  • You can see [an answer I wrote elsewhere](http://stackoverflow.com/a/16336070/1336654), and all the threads linked to the question of which I marked this as a duplicate. – Jeppe Stig Nielsen Dec 24 '14 at 08:39

2 Answers2

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.

Gnucki
  • 5,043
  • 2
  • 29
  • 44
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

dotnetstep
  • 17,065
  • 5
  • 54
  • 72
  • why in the first I will not get the name?I need to understand please – Miral Dec 24 '14 at 07:40
  • you are right but I need to understand why? as I thought that in both you send the current address we are in – Miral Dec 24 '14 at 07:42