Partial Class in C#

What is Partial Class?

Partial class is a feature in which it is possible to split the definition of class or structure or an interface over two or more source files. Each source files contain the section of class definition and all parts are combined when the application is compiled. This is only possible with the help of the partial keyword.

using the partial keyword indicates that other parts of class, struct or interface can be defined within namespace. All the parts must have same accessibility such as-internal, protected and so on so that all the parts properly combined when we compile the program.

Partial class is a feature added to C# 2.0 and visual studio 2005.

Example Illustrating Partial class

using system;

namespace MyNameSpace

{

    public partial class MyClass

    {

        public void disp1()

        {

            Console.WriteLine("hello I am method of partial class ");

        }

    }

    public partial class MyClass

    {

        public void disp2()

        {

            Console.WriteLine("hiiii I am method of partial class");

        }

    }

    class Tester

    {

        static void Main()

        {

            MyClass obj = new Myclass();

            obj.disp1();

            obj.disp2();

        }

    }

}

Output

hello I am method of partial class
hiiii I am method of partial class
Press any key to continue.....

Ebook Download
View all
Learn
View all