Skip to main content

Passing Parameters by Value and Reference with Example in C#

 Passing Parameters by Value and Reference with Example in C#

Passing a Value-Type parameter to a method by value means passing a copy of the variable to the method. So the changes made to the parameter inside of the called method will not have an effect on the original data stored in the argument variable.

 In C#, Value-Type variable contain the value directly on its memory and Reference-Type variable will contain a reference of its data.

 Passing Parameters by Value Example in C#

Following is the example of passing a value type parameter to a method by value in C#.

 

static void Main(string[] args)

        {

            int x = 10;

            Console.WriteLine($"Variable Value Before Calling the Method: {x}");

            Multiplication(x);

            Console.WriteLine($"Variable Value After Calling the Method: {x}");

                      

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

 

        }

        public static void Multiplication(int y)

        {

            y *= y;

            Console.WriteLine($"Variable Value Inside the Method: {y}");

        }

  

From the above example, the variable x is a value type and its passed to the Multiplication method. The content of variable x copied to the parameter y and made required modifications in the Multiplication method but the changes made inside of the method have no effect on the original value of the variable.

 

Value Type

 


When we execute the above C# program, we will get the result as shown above.

 

 

From above result, the variable value not changed even after we made the modifications in our method.

 

Pass parameter by Reference (ref) in C#

 

Passing a value type parameter to a method by reference means passing a reference of the variable to the method. So the changes made to the parameter inside of the called method will have an effect on the original data stored in the argument variable.

 

By using the ref keyword, we can pass parameters reference-type and it’s mandatory to initialize the variable value before we pass it as an argument to the method in C#.

 

Declaration of C# Pass By Reference

The following is a simple example of passing parameters by reference in C#.

 

int r = 10; // Variable need to be initialized

Multiplication(ref r);

 

If we observe the above declaration, we declared and assigned a value to the variable r before we pass it as an argument to the method by using reference (ref).

 

To use the ref parameter in C# application, both the method definition and the calling method must explicitly use the ref keyword.

 

Passing Parameters by Reference Example in C#

 

The code below is an example of passing a value type parameter to a method by reference in C#.

 

 static void Main(string[] args)

        {

            int x = 10;

            Console.WriteLine($"Variable Value Before Calling the Method: {x}");

            Multiplication(ref x);

            Console.WriteLine($"Variable Value After Calling the Method: {x}");

                      

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

 

        }

        public static void Multiplication(ref int y)

        {

            y *= y;

            Console.WriteLine($"Variable Value Inside the Method: {y}");

        }

From the above example, we are passing the reference of variable x to the variable y in Multiplication method by using the ref keyword. In this case, the variable contains the reference of variable x so the changes that made to the variable y will affect the value of variable x.

 

Reference Type


 

When we execute the above c# program, we will get the result as shown above.

 

If we observe the above result, the changes that we did for the variable in the called method have reflected in the calling method also.


References

1.    Tutlane

2.    Tutorial Teachers

Comments

Popular posts from this blog

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

Most Popular Programming Languages in 2020

Most Popular Programming Languages in 2020 In this blog post, you will learn about the most popular programming languages in 2020 for creating the best web applications. Check its pros and cons. Analyzed by technostacks Not very long ago, just a few people were considered to be computer programmers, and the general public viewed them with awe. In this digital age that we are now living in, however, a large number of IT jobs need a solid grasp of one or more programming languages. Whether one wants to develop a mobile app or get a certification for having programming knowledge, or even to learn new skills, one needs to opt for the right programming language. Below mentioned eight most popular programming languages which are in demand for software development and web applications. This is the most used programming languages in 2019 and will be in 2020. For each, there is little information about the language, benefits and its complexity, as well as about its usage. One must...

HashTable in C# with Example

  HashTable in C# with Example Hashtable  is used to store a collection of key/value pairs of different  data types  and are organized based on the hash code of the key.   Generally, the hashtable object will contain buckets to store elements of the collection. The bucket here, is a virtual subgroup of elements within the hashtable and each bucket is associated with a hash code, which is generated based on the key of an element.   In C#, hashtable is same as a  dictionary  object but the only difference is that the  dictionary  object is used to store a key-value pair of same  data type  elements.   When compared with  dictionary  object, the hashtable will provide a lower performance because the hashtable elements are of object type so the boxing and unboxing process will occur when we are storing or retrieving values from the hashtable.   C# HashTable Declaration Hashtable is a non-generic type...