Partial Class

.Partial Class


Partial Class Program Output

.Partial Class

"A C# program which shows how class definitions can be split into pieces by using the "partial" modifier."

This C# program splits the implementation of a class into two separate pieces by using the "partial" modifier. The partial class is then instantiated and data values are retrieved from each piece. The following C# features are used in the sample code:

  1. Partial Modifier - Allow for the definition of a class, struct, or interface to be split into multiple pieces, and stored in different files.
  2. Partial Classes - Visual Studio uses partial classes frequently split the generated code from the user defined code.

    Partial Class

    namespace partialclass
    {
        partial class PartialClass
        {
            private int _x = 1;
            public int X { get { return _x; } }
        }

        partial class PartialClass
        {
            private int _y = 2;
            public int Y { get { return _y; } }
        }

        class Program
        {
            static void Header()
            {
                System.Console.WriteLine("******************************************");
                System.Console.WriteLine("*** Partial Class with Split Variables ***");
                System.Console.WriteLine("******************************************");
            }
            static void Main()
            {
                Program.Header();

                PartialClass myClass = new PartialClass();

                System.Console.WriteLine("\n------------ First Partial Class ------------");
                System.Console.WriteLine("Variable X from first partial class has value: {0}", myClass.X);

                System.Console.WriteLine("\n------------ Second Partial Class ------------");
                System.Console.WriteLine("Variable Y from second partial class has value: {0}\n", myClass.Y);           
            }
        }
    }