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>)</parameters></delegate_name></return></access>
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.
The above is the detailed content of C# References. For more information, please follow other related articles on the PHP Chinese website!

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
