C# References

PHPz
PHPzOriginal
2024-09-03 15:26:23379browse

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:

C# References

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:

C# References

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:

C# References

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:

C# References

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.

The above is the detailed content of C# References. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Polymorphism in C#Next article:Polymorphism in C#