C# Reference Type Variable

In this chapter you will learn:
  • What is Reference Type?
  • What is Reference type parameter?
  • How to use Reference Type Parameter in C#?

The Reference type variable is such type of variable in C# that holds the reference of memory address instead of value. class, interface, delegate, array are the reference type. When you create an object of the particular class with new keyword, space is created in the managed heap that holds the reference of classes.

If you are passing reference type variable as parameter, then you will have to use ref keyword with variable.

 

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reference_Parameter
{
    class Program
    {
        public static void qube(ref int num)
        {
            num = num * num * num;
        }
        static void Main(string[] args)
        {
            int original = 9;
            Console.Write("\ncurrent value of Original is {0}\t", original);
            Program.qube(ref original);
            Console.WriteLine("\nNow the current value of Original is {0}\t", original);
            Console.ReadLine();
        }
    }
}

Output
current value of Original is 9
Now the current value of Original is 729
__
referencetype-parameter-flowchart

Summary

In this chapter you learned about reference type parameter and variables in C#. In next chapter you will learn output parameter in C sharp.

More Articles

 

Share your thought