Partial classes

A class is defined to be partial when we are working on the same class on multiple PCs. Later when the classes from different PCs are combined together, they compile to a single class. The class must have a partial keyword with it and the name of the class must be same in all PCs.

The following example has three files. In the first two files, the partial class is defined. In the third file, the partial class is used in Main function

Class1.cs

using System;
namespace ConsoleApplication6
{

    public partial class TheBigClass
    {
        public TheBigClass()
        {

        }
        public void MethodOne()
        {
            Console.WriteLine("This is method 1");
        }

        
    }
}

Class2.cs

using System;
namespace ConsoleApplication6
{
    public partial class TheBigClass
    {
  
        public TheBigClass(int a)
        {

        }
        public void MethodTwo()
        {
            Console.WriteLine("This is method 2");
        }

        
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;


namespace ConsoleApplication6
{
    
    class Program
    {
 

        static void Main(string[] args)
        {

           TheBigClass a = new TheBigClass();

            a.MethodOne();
            a.MethodTwo();
        }        
        
    }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *