Parameters Introduction

The original article can be found here at my blog.

As we know, C# is an Object Oriented Programming language and since it is object oriented the users of the objects need to interact with the data members of the objects. That can be done using the member functions of the class. Passing parameters to the member functions or the static functions of the class is an important part of programming, that is why it is very important to understand the ways in which we can pass the parameters to the functions in C#.

Before starting the article you may want to be familiar with the value types and reference types in C# that I have discussed in my one of my articles.

There are basically two ways in which we can pass parameters to the functions in C#.

  1. Pass by Value
  2. Pass by reference

I want to explain both of them one by one.

Pass By Value

Pass By Reference

Explaining out and ref keywords

As I have already said, parameters can be passed to the function by ref using the two keywords out and ref. In this portion of the article I want to explain the main differences between these two keywords.

  1. The metadata emitted by the CLR for both of these keywords is the same, that in turn indicates that the parameter is passed by the reference no matter which keyword is used.

  2. The CLR treats both of these keywords as identical and that can be confirmed by my previous point, but that is not the case with the C# compiler that treats these keywords differently.

  3. If the parameter is marked as out then there is no need for the caller to initialize the variable. The called method is not expected to read from the parameter value without being initialized and the parameter cannot be returned without being initialized. If we are not initializing the parameter in the method, we will get the compile time error.

  4. If the method uses the ref keyword and is not initialized before being passed to the method then that expects the parameter passed by ref and we will get a compile time error stating “Use of uninitialized local variable”, which in other words we need to initialize the value before using it with the ref keyword.

  5. It is generally preferred to use the out keyword if the variable being created is a large value type or reference type.

  6. We can create overloaded methods based on the ref keyword and without the ref keyword as shown in the following code snippet.
    1. public static void NullifyTheReference(ref MyClass myClass)
    2. {
    3. }
    4. public static void NullifyTheReference(MyClass myClass)
    5. {
    6. }
  7. On the other hand we cannot overload the methods based on the out and ref keywords since the CLR treats both of them as the same. The following code snippet is totally invalid and we will get a compile time error.
    1. public static void NullifyTheReference(ref MyClass myClass)
    2. {
    3. }
    4. public static void NullifyTheReference(out MyClass myClass)
    5. {
    6. }
  8. The variables passed by reference to the methods must be of the same type as of the parameter of the method, to ensure the type safety in .NET that I explained in one of my articles here.

    Before ending the article I would like to show the example of the out keyword
    1. static void Main(string[] args)
    2. {
    3. MyClass myclassInst;
    4. UsingTheOut(out myclassInst);//using the uninitialized variable myclassInst
    5. Console.WriteLine(myclassInst.LocalProperty);//prints 10
    6. Console.ReadKey();
    7. }
    8. public static void UsingTheOut(out MyClass myClass)
    9. {
    10. myClass = new MyClass();
    11. myClass.LocalProperty = 10;
    12. }

This was an article about the ways parameters can be passed to methods in C#. Though these keywords can be easy to understand we should use them carefully and intelligently to avoid some undesirable results.