Feature Top (Full Width)

Thursday, 7 June 2012

Partial Classes in C#


Partial Classes :

It is a New feature that has been add in c#2.0 that allows you to define a class on multiple files.

Partial classes allows us to spilit huge volumes of code into multiple files so that organating becomes easier as well as multiple programmers can work onthe some class at the same time


Partial classes are advised to be used in the places where a class contains code related to perfrom diff operations there separate

diff code into diff files so that they get physically separated but logically thay come under the same class

part1.cs :

using System;
namespace OOPSProject
{
    partial class Parts
    {
        public void Method1()
        {
            Console.WriteLine("Method 1");
        }
        public void Method2()
        {
            Console.WriteLine("Method 2");
        }
    }
}

part2.cs

using System;
namespace OOPSProject
{
    partial class Parts
    {
        public void Method3()
        {
            Console.WriteLine("Method 3");
        }
        public void Method4()
        {
            Console.WriteLine("Method 4");
        }
    }
}


using System;
namespace OOPSProject
{
    class TestParts
    {
        static void Main()
        {
            Parts p = new Parts();
            p.Method1(); p.Method2();
            p.Method3(); p.Method4();
            Console.ReadLine();
        }
    }
}


Note :
In case of partial class if we want to inherit from any other class it doesnt require to be done in all the files we need to do it in any single file

No comments:

Post a Comment