Tuesday, 5 January 2016

C#.NET Partial class

Partial class is an unfinished class.

Generally we cannot create two classes with the same name in a namespace. So, Every class you create is a finished class which cannot open again to add some code.
If you create a class as partial class then you can again create a class with the same name and the code inside both the classes will belong to the same class.

Example program to create partial classes and calling methods inside two partial classes with the same object

Program
using System;

namespace PartialClass
{
    class Program
    {
        static void Main(string[] args)
        {
            a o = new a();
            o.add();
            o.sub();
            Console.ReadKey();
        }
    }

    partial class a
    {
        public void add()
        {
            Console.WriteLine("add");
        }
    }
    partial class a
    {
        public void sub()
        {
            Console.WriteLine("sub");
        }
    }

}


Output

add

sub

No comments:

Post a Comment