Difference between out and ref parameters

Posted: March 19, 2014 in MS.Net Technology
Tags: , ,

I often get confused between out and ref parameters while writing code in C#. The differences are quite evident when you are dealing with value types.

Below are some interesting things about out and ref :-

  1. ref tells the compiler that the object is initialized before entering the function,
  2. ref is two-ways
  3. ref means that value is already set, method can read it and can modify it.
  4. out tells the compiler that the object will be initialized inside the function.
  5. out is out-only.
  6. out means that value isn’t set and method must set it before return and couldn’t read before setting value. Else it is not allowed to leave it unassigned
using System;

namespace RefOutDemo
{
    class Program
    {
        static void Main()
        {
            int val = 0;
            var emp = new Employee {Id = 1, Name = "Kishore", Salary = 100};

            Example1(val);
            Method1(emp);

            Console.WriteLine(val); // Still 0!
            Console.WriteLine(emp.Salary); // Now 1100!

            Example2(ref val);
            Method2(ref emp);
            Employee nEmployee;
            //Method2( ref nEmployee) - Compiler error

            Console.WriteLine(val); // Now 2!
            Console.WriteLine(emp.Salary); // Now 3100!

            Example3(out val);
            Method3(out emp);

            Console.WriteLine(val); // Now 3!
            Console.WriteLine(emp.Salary); // Now 100!

            Employee nEmployee1;
            Method3(out emp);
            Console.WriteLine(emp.Salary); // Now 100!

            Console.ReadLine();
        }

        static void Example1(int value)
        {
            value = 1;
        }

        static void Method1(Employee employee)
        {
            employee.Salary += 1000;
        }

        #region ref 
        static void Example2(ref int value)
        {
            value = 2;
        }

        static void Method2(ref Employee employee)
        {
            employee.Salary += 2000;
        }

        #endregion ref

        #region out
        static void Example3(out int value)
        {
            value = 3;
        }

        static void Method3(out Employee emp1)
        {
            //emp1 must be assigned upon exit
            emp1 = new Employee {Id = 11113, Name = "Kishore", Salary = 100};

        }

        #endregion out

    }

    class Employee
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public double Salary { get; set; }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s