The reference parameter is used to refer to the memory location of a variable. Unlike value parameters, which require a new storage location for the parameter, reference parameters represent the same memory location as the original parameters passed as arguments to the method. In C#, we use the keyword “ref” to declare reference parameters. When we pass reference arguments as parameters to a function in C#, we pass the reference to the memory location instead of the original value. We refer to this concept as “call by reference” in C#.
Syntax
ref data_typevariable_name
Where data_type is the data type of the variable with variable_name.
Working of C# Call By Reference
- Whenever there is a need to call a function that accepts arguments and if we want to pass the reference address of the variable in the memory location as an argument to the function, we use call by reference in C#.
- A reference parameter is a parameter that receives the address of a variable in memory.
Examples
Given below are the examples mentioned:
Example #1
C# program to demonstrate call by reference in which we calculate the square of a number and display the values before calling the function by reference and after the function is called.
Code:
using System; //a namespace called program1 is defined namespace program1 { //a class called check is defined class check { //a function is defined which takes reference variable as an argument public void displaypower(ref double value) { //the square of the passed value is found using pow method double power = Math.Pow(value,2); //The resulting value is added to the value passed as reference value = value + power; Console.WriteLine("Value when the control is inside the function "+value); } //main method is called static void Main(string[] args) { //a double variable is defined double value = 5; //an instance of the check class is defined which consists of the function taking reference parameter as an argument check check1 = new check(); Console.WriteLine("Value before the function is called "+value); //a function is called by reference check1.displaypower(ref value); Console.WriteLine("The value of the variable remains the same as inside the function because we are calling the function by reference " + value); } } }
Output:
Explanation:
- In the given program, we define the namespace “program1”. Inside this namespace, we have a class named “check” that contains a function. This function takes a reference variable as an argument.
- The function takes the value passed as a reference and calculates the square of the value and adds it to the actual value, and then displays the resulting value.
Example #2
C# program to demonstrate call by reference in which we convert the given string in small letters to capital letters by calling the function by reference and passing the string in small letters as a reference parameter.
Code:
using System; //a namespace called program1 is defined namespace program1 { //a class called check is defined class check { //a function is defined which takes reference variable as an argument public void displayupper(ref string value) { //ToUpper method is used to convert the string from small letters to capital letters value = value.ToUpper(); Console.WriteLine("Value when the control is inside the function "+value); } //main method is called static void Main(string[] args) { //a double variable is defined string value = "shobha"; //an instance of the check class is defined which consists of the function taking reference parameter as an argument check check1 = new check(); Console.WriteLine("Value before the function is called "+value); //a function is called by reference check1.displayupper(ref value); Console.WriteLine("The value of the variable remains the same as inside the function because we are calling the function by reference " + value); } } }
Output:
Explanation:
- In the above program, I define a namespace called “program1”. Then, I define a class called “check” within this namespace. Inside the “check” class, I define a function that takes a reference variable as an argument.
- The function takes the value passed as a reference and converts the given string in small letters to capital letters using ToUpper() method.
Recommended Articles
This is a guide to C# Call By Reference. Here we discuss the introduction with working and programming examples. You may also have a look at the following articles to learn more –
- C# Dictionary
- DataReader C#
- IEnumerable C#
- C# System.IO
The above is the detailed content of C# Call By Reference. For more information, please follow other related articles on the PHP Chinese website!

The latest developments and best practices in C#.NET development include: 1. Asynchronous programming improves application responsiveness, and simplifies non-blocking code using async and await keywords; 2. LINQ provides powerful query functions, efficiently manipulating data through delayed execution and expression trees; 3. Performance optimization suggestions include using asynchronous programming, optimizing LINQ queries, rationally managing memory, improving code readability and maintenance, and writing unit tests.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

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.


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

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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