Csharp/CSharp Tutorial/Language Basics/ref

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Change string using ref keyword

<source lang="csharp">using System; class MainClass {

 public static void UpperCaseThisString(ref string s)
 {
   s = s.ToUpper();
 }
 public static void Main() 
 {
   string s = "str";
   Console.WriteLine("-> Before: {0}", s);
   UpperCaseThisString(ref s);
   Console.WriteLine("-> After: {0}\n", s);
 }

}</source>

-> Before: str
-> After: STR

Passing Parameters by Value

<source lang="csharp">public class Swapper {

   public void Swap(int x, int y) {
       System.Console.WriteLine("In Swap(): initial x = " + x + ", y = " + y);
       int temp = x;
       x = y;
       y = temp;
       System.Console.WriteLine("In Swap(): final   x = " + x + ", y = " + y);
   }

}

class MainClass {

   public static void Main() {
       int x = 2;
       int y = 5;
       System.Console.WriteLine("In Main(): initial x = " + x + ", y = " + y);
       Swapper mySwapper = new Swapper();
       mySwapper.Swap(x, y);
       System.Console.WriteLine("In Main(): final   x = " + x + ", y = " + y);
   }

}</source>

Swap two references.

<source lang="csharp">using System;

class RefSwap {

 int a, b; 
  
 public RefSwap(int i, int j) { 
   a = i; 
   b = j; 
 } 

 public void show() { 
   Console.WriteLine("a: {0}, b: {1}", a, b); 
 } 

 // This method changes its arguments. 
 public void swap(ref RefSwap ob1, ref RefSwap ob2) { 
   RefSwap t; 
 
   t = ob1; 
   ob1 = ob2; 
   ob2 = t; 
 } 

}

class MainClass {

 public static void Main() { 
   RefSwap x = new RefSwap(1, 2); 
   RefSwap y = new RefSwap(3, 4); 

   Console.Write("x before call: "); 
   x.show(); 

   Console.Write("y before call: "); 
   y.show(); 

   Console.WriteLine(); 

   // exchange the objects to which x and y refer 
   x.swap(ref x, ref y);  

   Console.Write("x after call: "); 
   x.show(); 

   Console.Write("y after call: "); 
   y.show(); 

 } 

}</source>

x before call: a: 1, b: 2
y before call: a: 3, b: 4
x after call: a: 3, b: 4
y after call: a: 1, b: 2

Swap two values

<source lang="csharp">using System;

class Swap {

 // This method now changes its arguments. 
 public void swap(ref int a, ref int b) { 
   int t; 
 
   t = a; 
   a = b; 
   b = t; 
 } 

}

class MainClass {

 public static void Main() { 
   Swap ob = new Swap(); 

   int x = 10, y = 20; 

   Console.WriteLine("x and y before call: " + x + " " + y); 

   ob.swap(ref x, ref y);  

   Console.WriteLine("x and y after call: " + x + " " + y); 
 } 

}</source>

x and y before call: 10 20
x and y after call: 20 10

Use out for reference type

<source lang="csharp">using System; class MyClass {

  public int Val = 20;                     

} class MainClass {

  static void MyMethod(out MyClass f1, out int f2)
  {
     f1 = new MyClass();                   
     f1.Val = 25;                          
     f2 = 15;                              
  }
  static void Main()
  {
     MyClass myObject = null;
     int intValue;
     MyMethod(out myObject, out intValue);             
     Console.WriteLine("After  -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
  }

}</source>

After  -- myObject.Val: 25, intValue: 15

Use ref for int value

<source lang="csharp">using System;

class MyClass {

  public int Val = 20;                  

} class MainClass {

  static void MyMethod(ref MyClass myObject, ref int intValue)
  {
     myObject.Val = myObject.Val + 5;               
     intValue = intValue + 5;                       
  }
  static void Main()
  {
     MyClass myObject = new MyClass();
     int intValue = 10;
     Console.WriteLine("Before -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
     MyMethod(ref myObject, ref intValue);          
     Console.WriteLine("After  -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
  }

}</source>

Before -- myObject.Val: 20, intValue: 10
After  -- myObject.Val: 25, intValue: 15

Use ref for reference type

<source lang="csharp">using System;

class MyClass {

  public int Val = 20;                  

} class MainClass {

  static void MyMethod(ref MyClass myObject, ref int intValue)
  {
     myObject.Val = myObject.Val + 5;               
     intValue = intValue + 5;                       
  }
  static void Main()
  {
     MyClass myObject = new MyClass();
     int intValue = 10;
     Console.WriteLine("Before -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
     MyMethod(ref myObject, ref intValue);          
     Console.WriteLine("After  -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
  }

}</source>

Before -- myObject.Val: 20, intValue: 10
After  -- myObject.Val: 25, intValue: 15

Use ref to pass a value type by reference

The ref parameter modifier causes C# to create a call-by-reference, rather than a call-by-value.


<source lang="csharp">using System;

class RefTest {

 /* This method changes its argument. 
    Notice the use of ref. */ 
 public void sqr(ref int i) { 
   i = i * i; 
 } 

}

class MainClass {

 public static void Main() { 
   RefTest ob = new RefTest(); 

   int a = 10; 

   Console.WriteLine("a before call: " + a); 

   ob.sqr(ref a); // notice the use of ref 

   Console.WriteLine("a after call: " + a); 
 } 

}</source>

a before call: 10
a after call: 100