Saturday, September 4, 2010

C#.net - Partial Classes

.Net 2.0 provided us with new feature called partial classes. Using partial classes we can use multiple files to keep the code of same class. we can put some methods to one file and the others to another file.
The developer can then extend the code without disturbing the base code. Teams of developers collaborating on an application also benefit from partial classes. The class can be parceled based on responsibility. The developer works in separate source files, which isolate changes and focus a developer on code relevant to their responsibility.

Consistency is the key to partial types:

  • All partial classes name must be same.
  • Each partial type is preceded with the partial keyword.
  • The partial types must have the same accessibility.
  • If any part is sealed, the entire class is sealed.
  • If any part is abstract, the entire class is abstract.
  • Inheritance at any partial type applies to the entire class.
Let See how to create a partial classes in C#.net

Step 1
Create a Console application and give the project name as a SolPartialClass.

Step 2
Create a partial classes. The parts of this class can be also defined in same file if needed.


public partial class PartialDemoClass
    {
        #region Method

        public void MethodA()
        {
            try
            {
                System.Console.WriteLine("Method A calling");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion
    }

    public partial class PartialDemoClass
    {
        #region Method

        public void MethodB()
        {
            try
            {
                System.Console.WriteLine("Method B Calling");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion
    }

Step 3
Create a instance of parital class in program class,it is look like this

 class Program
    {
        static void Main(string[] args)
        {
            try
            {
                PartialDemoClass Obj = new PartialDemoClass();

                Obj.MethodA();
                Obj.MethodB();  
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);    
            }
        }
    }

Full Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SolPartialClass
{
    public partial class PartialDemoClass
    {
        #region Method

        public void MethodA()
        {
            try
            {
                System.Console.WriteLine("Method A calling");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion
    }

    public partial class PartialDemoClass
    {
        #region Method

        public void MethodB()
        {
            try
            {
                System.Console.WriteLine("Method B Calling");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion
    }


    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                PartialDemoClass Obj = new PartialDemoClass();

                Obj.MethodA();
                Obj.MethodB();  
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);    
            }
        }
    }

   
}

Download
Download Source Code

No comments:

Post a Comment