ref parameters

In this chapter you will learn:

  1. Use ref to pass a value type by reference
  2. Swap two values
  3. Swap two references
  4. Change string using ref keyword

Pass integer by ref

To pass by reference we use the ref parameter modifier. We add the ref modifier in front of the parameter.

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

using System;/*j  av a  2 s .c o m*/

class MainClass
{
  static void Main(string[] args)
  {
      int MyInt = 5;

    MyMethodRef(ref MyInt);
   
        Console.WriteLine(MyInt);
  }
  
  static public int MyMethodRef(ref int myInt)
  {
    myInt = myInt + myInt;
    return myInt;
  }
}

The code above generates the following result.

The ref keyword is required in the method declaration as well as the method call.

From the output we can see that the int value in the main method is changed by the method MyMethodRef since the pointer to the int value is passed into the MyMethodRef method.

Swap two values

We can use the ref parameter modifier to exchange value of two parameters.

using System; /*from  j a v  a2 s. c  om*/
 
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); 
  } 
}

The code above generates the following result.

Use ref with reference types

We can use ref modifier for reference type value as well.

using System;//  ja v a 2 s . c om
class Rectangle
{
    public int Width = 5;
    public int Height = 5;

}

class Program
{
    static void change(ref Rectangle r)
    {
        r = null;
    }

    static void Main(string[] args)
    {


        Rectangle r = new Rectangle();
        Console.WriteLine(r.Width);
        change(ref r);
        Console.WriteLine(r == null);
    }
}

The output:

If we use the ref modifier in front of a reference type what we get in the method is the address of the reference or the reference of the reference.

Change string using ref keyword

using System;/*from  jav a  2 s  .com*/

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);
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Use out for int type
  2. Use out for reference type
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances