Adnan Mohsin January 15, 2017 Comments 2.00

"Ref and Out parameters in C Sharp

Normally, when we pass an argument to the method then it is initialized with the copy of that argument. But we have an option of handing off the original variable by just copying its data with the help of ref and out keywords in c sharp programming.

We can pass parameters to the methods by two ways:

  • First is by value type in which the content we get in parameter is the original data it’s the default behavior of methods in c #.
  • Second way is by reference type that we get a copy of the reference which points to the same object in reference memory (ie heap)

The use of ref and out parameter is simple. We can put ‘ref’ and ‘out’ keyword before the method variable.

Using ref parameter in C#

The ref keyword is used to pass the parameter by reference to a method in C#. In this case the reference memory is used and no new memory location is created unlike  the case in default value type parameter.

Following example illustrates the use of ref keyword in c#

using System;

namespace RefandOutDemo
{
    class Program
    {
        static void Increment(int val)
        {
            val++;
        }

        static void Increment(ref int val)
        {
            val++;
        }
        static void Main(string[] args)
        {
            int counter = 67;
            Increment(counter);
            Console.WriteLine("The Counter value is: {0}" ,counter);

            Increment(ref counter);
            Console.WriteLine("The Counter value By Ref is: {0}", counter);
            Console.ReadKey();
        }
    }
}

 

Using Out parameter in C#

When we use ref parameter we first assign a value before calling method, but there might be times when we want the method to initialize the parameter for that we have a keyword ‘Out’. The out keyword is similar to the ref keyword in usage such that we can prefix a parameter with the out keyword so that the parameter becomes an alias for that argument. The keyword out is short for output. When we pass an out parameter to a method, the method must assign a value to it without creating the new storage location. The out parameter is useful when the method returns nothing and we want something to get back as a result from that method.

 The following example illustrates the demo for Out Keyword usage in c#:

using System;

namespace RefandOutDemo
{
    class Program
    {
        static void Multiply(int X, out int Y)
        {

            Y = X*X; //Squared result is stored into the Y ouput parameter  

        }
        static void Main(string[] args)
        {
            int result;
            Console.WriteLine("Enter a value to get its square:");
            var getValue = Console.ReadLine();
            Multiply(int.Parse(getValue), out result);
            Console.WriteLine("The value is: {0}" ,result);

            Console.ReadKey();
        }
    }
}

 

Design
8/10
Support
2/10
Development
4.5/10

7.5

Awesome
3.50
Summary

I hope this article is useful for understanding the concept of Ref and Out keywords. For suggestion and feedback regarding this article then please comment or contact me.

Search widget

Banner

Latest posts

Design Patterns Design Patterns in Programming

What is a Design Pattern? According to MSDN, "A design pattern is a description of a set of interacting classes that provide a framework for a solution to a generalized problem in a specific context or environment. In other words, a pattern suggests a solution to a particular problem or issue in object-oriented software development.”

May 05, 2017 16 0

C# Ref and Out parameters in C Sharp

This tutorial explains the concepts of Ref and Out Keywords in C # programming. These two keywords are also frequently asked in most interviews.

January 15, 2017 16 0

WPF Vector Graphics in WPF

In this article, I will discuss how we can create a Line, Rectangle, ellipse and Polygon control in WPF at design-time using XAML and at run-time using C#.I will also explain their properties to format such as fill property. After that, i will show you to set an image as the background of any shape object.

January 08, 2017 16 0

C# What is the difference between an Abstract Class and an Interface

One question is always out there in most interviews. What is the difference between an Abstract Class and an Interface and why do you use one over the other? It seems like a simple question but most often, the answer to this question is not sufficient for the interviewer and can cost a job. So, I have decided to write an article about them and have tried to explain it.

December 26, 2016 16 0