Updated April 10, 2023
Introduction to C# References
A memory location of a variable can be referenced by using a parameter called reference parameter in C# and they are different from parameters called value parameters in which the values are passed as parameters and a new memory location is created for these values whereas, in reference parameters, no memory location is allocated for these parameters as only the reference of these parameters are passed and the reference parameters can be declared by using the keyword ref.
The syntax to declare the reference parameter in C# is as follows:
return type Method name(ref arg1, ref arg2)
Declare local variables, for example a, b
Calling the method,
Method name(ref a, ref b)
The above syntax represents declaring reference parameters in C#. The reference parameters can be declared by using the keyword ref and they can be accessed by using the keyword ref.
Working of Reference Parameters in C#
Consider the following program which consists of two methods add and subtract. The add method accepts the parameter passed by value and the subtract method accepts the parameter passed as a reference. Initially, two variables are declared and initialized with two values. Then add method is called by passing the value as the parameter. There is no change in the value even though the method is supposed to perform the operation on the value passed as a parameter because this passes by value. The next subtract method is called to which the reference parameter is passed. The operation defined in the subtract method is performed on the value passed as a reference and it is updated.
Code:
using System;
namespace refer {
public class check {
// Calling the main method
public void Main(string[] args)
{
// The values of a and b are initialized
int a = 15, b = 20;
// The values of a and b are displayed before making changes to the values
Console.WriteLine("value of a before changing is {0}", a);
Console.WriteLine("value of b before changing is {0}", b);
Console.WriteLine();
// The add method is called by passing the value
add(a);
// After calling the method by value, the changes value is displyed
Console.WriteLine("After calling the add function"+
" value of a is {0}", a);
// The sub method is called by passing the reference
subtract(ref b);
// Display modified value of b
Console.WriteLine("Value of b after "+
"subtration operation is {0}", b);
}
// Defining the add method which accepts value as parameters
public static void add(int a)
{
a += 5;
}
// Defining the subtract method which accepts reference as parameters
public static void subtract(ref int b)
{
b -= 5;
}
}
}
Output:
Types of References in C#
Here are the following Types of References in C#
1. Class
Class is one of the C# reference types and they can be declared using the keyword class. The syntax to declare a class in C# is shown below:
Class classname
{
}
The class supports inheritance. That is a class can inherit the implementation of the base class. Classes can be either public, private, protected. The following program demonstrates the creation of the class.
Code:
using System;
namespace check
{
class children
{
private int height;
private string names;
// This is the consturctor class which is default
public children()
{
names = "nobody";
}
// Defining a constructor with arguments
public children(string names, int height)
{
this.names = names;
this.height = height;
}
// The method to print the values
public void Print()
{
Console.WriteLine("{0} is {1} inches tall.", names, height);
}
}
class Test
{
static void Main()
{
// New operator is used to create the objects on the class
children child1 = new children("Shobha", 5);
children child2 = new children("Ravi", 6);
// An object is created using default constructor
children child3 = new children();
// The results are displayed
Console.Write("The first child: ");
child1.Print();
Console.Write("The second child: ");
child2.Print();
Console.Write("The third child: ");
child3.Print();
}
}
}
Output:
2. Interface
A contract is defined using an interface. The members of any class have a definite implementation provided by the interface. The following program demonstrates the creation and implementation of the interface.
Code:
using System;
//An interface is defined
interface Point
{
int A
{
get;
set;
}
int B
{
get;
set;
}
double Dist
{
get;
}
}
//A class is implementing the interface
class Pointed : Point
{
// Constructor of the class
public Pointed(int a, int b)
{
A = a;
B = b;
}
public int A { get; set; }
public int B { get; set; }
public double Dist =>
Math.Sqrt(A * A + B * B);
}
class Maincl
{
static void PrintPointed(Point r)
{
Console.WriteLine("a={0}, b={1}", r.A, r.B);
}
static void Main()
{
Point r = new Pointed(2, 3);
Console.Write("the points are: ");
PrintPointed(r);
}
}
Output:
3. Delegate
The declaration of a delegate type is like the declaration of a method. It returns a value and it can take any number of arguments of any type as parameters. It is basically used in the encapsulation of methods acting as a pointer to a function. A delegate can be declared using the delegate keyword. The syntax to declare the delegate is as follows:
<access modifier> delegate <return type> <delegate_name>(<parameters>)
Consider the below program demonstrating the creation of delegates
Code:
using System;
class Program
{
// Creating delegates
public delegate void Printdel(int values);
static void Main(string[] args)
{
// We are printing a number using delegates
Printdel print = PrintNum;
print(100);
print(20);
//We are printing money using printmon delegate
print = PrintMon;
print(10);
print(20);
}
public static void PrintNum(int number)
{
Console.WriteLine("The Number is: {0,-12:N0}",number);
}
public static void PrintMon(int mon)
{
Console.WriteLine("The Money is: {0:C}", mon);
}
}
Output:
Conclusion
In this tutorial, we understand the concept of References in C# through definition and then understand the syntax and types of references in C# through example programs.
Recommended Articles
This is a guide to C# References. Here we discuss the Types of References in C# along with the syntax and the working of the reference parameter. You may also have a look at the following articles to learn more –