Difference between value type and reference type


Value type:

The value type based objects directly contains the value. Here there is no need to create instance with values.

The value type variable's are 
  1. struct
  2. enum
Example

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

namespace ConsoleApplication1
{
    class Program
    {
        struct Point
        {
            private int x, y;
            public Point(int x, int y)
            {
                this.x = x;
                Console.WriteLine(x);
                Console.ReadLine();
                this.y = y;
            }
            public int X
            {
                get { return x; }
                set { x = value; }
            }
            public int Y
            {
                get { return y; }
                set { y = 3; }
            }
        }
        static void Main(string[] args)
        {
            Point p = new Point(2, 3);
            p.X = 9;
            int x=p.X;
        }
    }
}

In above program we have used structure variable like,

Point p = new Point(2, 3);

Here we are passing value to x and y. So, we calling this as value type.

Reference Type:

The reference type variales will create only instance but pass any values.

Example:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Application.Run(new frm());
        }
    }
    class frm : Form
    {
        public frm()
        {
            Button b = new Button();
            this.Controls.Add(b);
            b.Click+=new EventHandler(b_Click);
        }
        void b_Click(object sender, EventArgs e)
        {
            frm1 f = new frm1();
            f.Show();
        }
    }
    class frm1 : Form
    {
    }  
}

In the above program we created instance to open the another form and other form variables accessed only after creating instance. 

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all