Parameters, ref and out modifiers in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 3513 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Variables, Stack, Heap and default values in C# before this article.

Introduction

So far, we have seen variables in C#. Now let's discuss with the parameters of C# in this chapter.

Objective

The main objective of this article is to learn parameters in C# and also ref and out modifiers in C# programming.

Parameter

A method has a sequence of parameters that defines the set of arguments. These arguments can be passed to the parameters in two ways which are by value or by reference.

Let's take an example code,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static void Exam(int e)
        {
            e = e + 1;
            Console.WriteLine(e);    // prints 9
            e++;                     // Increments
            Console.WriteLine(e);    // prints 10
        }
        static void Main()
        {
            Exam(8);                 // prints 10 as calls the method Exam().
            Console.Write("\n");
            Exam(9);                 // prints 11 as it calls the method Exam().
        }
    }
}
In the above code, the method Exam has a single parameter e with the type int. We are calling the Exam in Main method which executes the entire function at every time we call.

Note: please read the comments in the code.

The output of this code will be,


ref Modifier

ref modifiers are passed as references and cannot be passed as values. We can assign them in the called method as well as at the calling site. These are changed at the calling site.

                               

This modifier is very essential in the implementation of swapping methods.
This is required at writing and also at calling a method.

Example:
Below example code explains the usage of ref modifier in C#,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static void Change(ref string str1, ref string str2)
        {
            string demo = str1;
            str1 = str2;
            str2 = demo;
        }
        static void Main()
        {
            string book = "Book";
            string paper = "Paper";
            Change(ref book, ref paper);

            Console.WriteLine(book);        // Paper
            Console.WriteLine(paper);       // Book
        }
    }
}
In the above example, we have a function that interchanges two strings that used ref modifiers.
The output will swaps the two strings as follows,


Observe the output in which 'Paper' has printed first.

out Modifier

out is used in two contexts i.e. as a parameter modifier and in the generic type declarations in delegates and interfaces.
The out keyword causes the arguments to be passed by the reference.


                        


  • out need not be assigned before going to a method.
  • It must be assigned before it comes out of the method.
Example:
Let's have an example of out modifier,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        static int a;
        static void Main()
        {
            Exam(out a);
        }

        static void Exam(out int b)
        {
            Console.WriteLine(a);       // 0  : Default value is 0
            b = 1;
            Console.WriteLine(a);       // 1  : Calling b indirectly, so it prints the b value
            b = 2;
            Console.WriteLine(a);       // 2  : Calling b indirectly, so it prints the b value
            b = 1;
            Console.WriteLine(a);       // 1  : Calling b indirectly, so it prints the b value
        }
    }
}
In the above code, we are assigning out after it went to a method. 

Note: please read the comments in the code.

Output of this code will be,


Conclusion

In this article, we have seen parameters of C# and also ref, out modifiers with examples. Hope you understand.

Thanks for reading,

Regards,
Krishna.
Recommendation
Read var type, Expressions and Precedence in C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)