Advertisement
//C# Example of ref parameter
using System;
class A
{
//Defining the method Add() to add 10 to the passed value type(int)
//by using the ref parameter
void Add(ref int i)
{
i = i + 10;
}
//Defining the Main() method
public static void Main(String[] ar)
{
//A local variable
int a = 10;
//Creating an object of A class
A ob = new A();
//Printing the value of local variable,a
//Before calling the Add() method
Console.WriteLine("Before calling the Add() method, the value in a is : " + a);
//The Add() method is called and passed a value type(int) by reference
//by using the ref parameter
ob.Add(ref a);
//Printing the value of local variable,a
//After calling the Add() method
Console.WriteLine("Afrer calling the Add() method, the value in a is : " + a);
}
}
Before calling the Add() method, the value in a is : 10
Afrer calling the Add() method, the value in a is : 20
Advertisement
//C# Example of using the ref parameter
//to swap or interchange the value in two char value type variables
using System;
class A
{
//Defining the Swap_Char() method to swap characters
//by using the ref parameter
void Swap_Char(ref char c, ref char d)
{
char e;
e = c;
c = d;
d = e;
}
//Defining the Main() method
public static void Main()
{
//Creating an object of class A
A ob = new A();
//Two local char variables
char a ='x';
char b ='y';
//Printing the local char variables before calling the Swap_Char() method
Console.WriteLine("The character value in a is : " + a);
Console.WriteLine("The character value in b is : " + a);
//The Swap_Char() method is called and passed two value type(char) values by reference
//by using the ref parameter
ob.Swap_Char(ref a, ref b);
Console.WriteLine("After the Swap_Char() method is called");
//Printing the local char variables after calling the Swap_Char() method
Console.WriteLine("The character value in a is : " + a);
Console.WriteLine("The character value in b is : " + b);
}
}
The character value in a is : x
The character value in b is : x
After the Swap_Char() method is called
The character value in a is : y
The character value in b is : x
//C# To modify the references, passed to a method using the ref parameter
using System;
class A
{
//Instance variable
int i;
//Defining the method Mod_Ref() to modify the passed references
//by using the ref parameter
void Mod_Ref(ref A ob1, ref A ob2)
{
Console.WriteLine("The Mod_Ref() method is called to modify the passed references");
A ob;
ob = ob1;
ob1 = ob2;
ob2 = ob;
}
//Defining the Main() method
public static void Main(String[] ar)
{
//Creating the first object of A class
A obj1 = new A();
//Initializing the instance variable of the first object
obj1.i = 100;
//Creating the second object of A class
A obj2 = new A();
//Initializing the instance variable of the second object
obj2.i = 200;
//Printing the value of instance variable, i, of the first object
//Before calling the Mod_Ref() method
Console.WriteLine("The value of instance variable, i, of the first object is : " + obj1.i);
//Printing the value of instance variable, i, of the second object
//Before calling the Mod_Ref() method
Console.WriteLine("The value of instance variable, i, of the second object is : " + obj2.i);
//The Mod_Ref() method is called and passed two references, by reference
//by using the ref parameter
obj1.Mod_Ref(ref obj1, ref obj2);
//Printing the value of instance variable, i, of the first object
//Before calling the Mod_Ref() method
Console.WriteLine("The value of instance variable, i, of the first object is : " + obj1.i);
//Printing the value of instance variable, i, of the second object
//Before calling the Mod_Ref() method
Console.WriteLine("The value of instance variable, i, of the second object is : " + obj2.i);
}
}
The value of instance variable, i, of the first object is : 100
The value of instance variable, i, of the second object is : 200
The Mod_Ref() method is called to modify the passed references
The value of instance variable, i, of the first object is : 200
The value of instance variable, i, of the second object is : 100
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement