Objects are passed by reference : Parameters Passing : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Parameters Passing »

 

Objects are passed by reference









    

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Objects are passed by reference. 
 
using System; 
 
class Test { 
  public int a, b; 
 
  public Test(int i, int j) { 
    a = i; 
    b = j; 
  }
  /* Pass an object. Now, ob.a and ob.b in object 
     used in the call will be changed. */ 
  public void change(Test ob) { 
    ob.a = ob.a + ob.b; 
    ob.b = -ob.b; 
  } 
} 
 
public class CallByRef { 
  public static void Main() { 
    Test ob = new Test(15, 20); 
 
    Console.WriteLine("ob.a and ob.b before call: " + 
                       ob.a + " " + ob.b); 
 
    ob.change(ob); 
 
    Console.WriteLine("ob.a and ob.b after call: " + 
                       ob.a + " " + ob.b); 
  } 
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Parameters Passing