Algorithm


In C#, a partial class is a class that can be split into multiple source files. Each source file contains a portion of the class definition, and all the parts are combined at compile time to create a single class. This feature is useful when you want to organize a large class into multiple files or when you need to separate auto-generated code from your custom code.

 

Code Examples

#1 C# Programming Partial Class and Partial Method

Code - C# Programming

namespace HeightWeightInfo
{
    class File1
    {
    }
    public partial class Record
    {
        private int h;
        private int w;
        public Record(int h, int w)
        {
            this.h = h;
            this.w = w;
        }
    }
}
Copy The Code & Try With Live Editor

#2 C# Programming Partial Class and Partial Method

Code - C# Programming

namespace HeightWeightInfo
{
    class File2
    {
    }
    public partial class Record
    {
        public void PrintRecord()
        {
            Console.WriteLine("Height:"+ h);
            Console.WriteLine("Weight:"+ w);
        }
    }
}
Copy The Code & Try With Live Editor

#3 C# Programming Partial Class and Partial Method

Code - C# Programming

namespace HeightWeightInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Record myRecord = new Record(10, 15);
            myRecord.PrintRecord();
            Console.ReadLine();
        }
    }
}
Copy The Code & Try With Live Editor

#4 Introduction to Partial Methods

Code - C# Programming

public partial class Car
{
    partial void InitializeCar();
    public void BuildRim() { }
    public void BuildWheels() { }
}
Copy The Code & Try With Live Editor

#5 Introduction to Partial Methods

Code - C# Programming

public partial class Car
{
    public void BuildEngine() { }
    partial void InitializeCar()
    {
        string str = "Car";
    }
}
Copy The Code & Try With Live Editor
Advertisements

Demonstration


C# Programming Partial Class and Partial Method