.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
abhays
satyapriyanayak
SP
Ashutosh Jha
Mahesh

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

What is Partial class in .NET?

Posted By:Shivprasad koirala       Posted Date: September 05, 2011    Points: 200    Category: .Net Framework    URL: http://www.questpond.com  

This article will discuss about Partial class in .NET.
 

Partial Class: - Partial class allows you to define a single class in to multiple physical files (more than one physical file) and during compilation time these classes are combined to form a single class.

Let's first see in which scenario Partial class are applicable.

Below is a simple BigClass which has three methods.

        public partial class BigClass
        {
             public void Add( )
            {
                //Logic for adding
            }
             public void Substract( )
            {
               //Logic for Substrating
            }
        	 public void Multiply( )
            {
		//Logic for multiplication
            }
        }

Now, in above code snippet class you can see that the BigClass containing three methods which will have their own implementation and the implementation can be bigger which can make the code not readable so partial basically divide this thing and make your code readable.

Let's demonstrate a simple example to see how exactly Partial Class help us make your code more readable for that just follow the following steps.

The below example will give you an idea of how does Partial Class look like.

namespace PartialExampleConsole
{
    class Program
    {
         public partial class BigClass 
        {
             public void Add()
            {
               //Implementation...
            }
        }
         public partial class BigClass
        {
             public void Substract()
            {
                //Implementation...
            }
        }
         public partial class BigClass
        {
             public void Multiply(int x, int y)
            {
                //Implementation...
            }
        }
    }
}

Step1: - create a Console application for that Go To > File > New > Project > Windows > New Console Application.

Step2: - Just add the below code snippet in your .cs file.

namespace PartialExampleConsole
{
    class Program
    {
        // Below you can see that i have declared the class as Partial.
        public partial class BigClass 
        {
            int ResultAdd;
            // created a add method which will give result of two numbers.
            public void Add(int i, int j)
            {
                ResultAdd = i + j;// Addition is done here.
                //The below line will print the output
                Console.WriteLine("The Substration of two Numbers is :" + ResultAdd);
            }
        }
        // Below you can see that i have declared the class as Partial 
        //with the same name as the above partial class.
        public partial class BigClass
        {
            int ResultSubstract;
            // created a Subtract method which will give result of two numbers.
            public void Substract(int a , int b)
            {
                ResultSubstract = a - b;//Substraction is done here.
                //The below line will print the output
                Console.WriteLine("The Addition of two Numbers is:" +ResultSubstract);
            }
        }
        // Below you can see that i have declared the class as Partial 
           with the same name as the above partial class.

        public partial class BigClass
        {
            int ResultMultiply;
            // created a Multiply method which will give result of two numbers.
            public void Multiply(int x, int y)
            {
                ResultMultiply = x * y;//Multiplicaton is done here
                //The below line will print the output
                Console.WriteLine("The Multiplication of two Numbers is :" + ResultMultiply);
            }
        }


        static void Main(string[] args)
        {
            //Created the object of BigClass.
            BigClass obj = new BigClass();
            obj.Add(5, 5);//Call the Add method and assign the value to the paramaters.
            obj.Substract(10, 5);//Call the Substract method and assign the value to the paramaters.
            obj.Multiply(5, 5);//Call the Multiply method and assign the value to the paramaters.
            Console.ReadLine();
        }
    }
}

Note: - In the main class I have created instance of the BigClass only once but have created three classes declared as Partial which means a single class can be span in to more files but at compiled time this classes are combined to form a single class.

Recently I recorded a very interesting video on What is the use of VAR key word in C#? . Do watch the video to understand in what scenarios we will use the var keyword in C#.

You can also view by clicking my other C# interview questions.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend