Partial Method

.Partial Method


Partial Method Program Output

.Partial Method

"A C# program which uses a partial method to split the method signature and implementation. If the partial method is not implementation, the compiler removes the signature at compile time."

This program defines a partial method within a partial class. The partial method contains a parameter passed by reference and an overloaded constructor. An object initializer is used when the object is created. The partial method changes the value of the parameter within the scope of the method call, as shown by the change in the "a" variable within the Main method. The value of the "y" variable is set by the constructor, while the value of the "z" variable is set by the object initializer. The following C# features are used in the sample code:

  1. Partial Method - A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type.
  2. Partial Classes and Methods - Partial methods are especially useful as a way to customize generated code.
  3. Object Initializer - Used to initialize type objects in a declarative manner. You must use an object initializer if you're defining an anonymous type.
  4. Constructors - Constructors are methods which have the same name as the class or struct, and they usually initialize the data members of the new object.
  5. this Keyword - Refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method
  6. Parameters - By default parameters are passed by value. The "ref" keyword defines and Input/output parameter, while the "out" keyword defines and output parameter.

    Partial Method

    namespace partialMethod
    {
        partial class ThePartialClass
        {
            // Signature for Partial Method
            partial void ThePartialMethod(ref int x);
        }

        partial class ThePartialClass
        {
            int z = 0;
            int y = 0;
            public ThePartialClass() { }
            public ThePartialClass(int y)
            {
                this.y = y;
            }
            // Implementation for Partial Method
            partial void ThePartialMethod(ref int x)
            {
                // Prints: PartialMethod: x=10,y=2,z=3
                System.Console.WriteLine("ParitalMethod: x={0},y={1},z={2}\n", x, y, z);
                x *= 2;
            }

            static void Header()
            {
                System.Console.WriteLine("*****************************************");
                System.Console.WriteLine("*** Partial Method with ref Parameter ***");
                System.Console.WriteLine("*****************************************\n");
            }

            static void Main()
            {
                int a = 10;

                ThePartialClass.Header();

                ThePartialClass myPartialClass = new ThePartialClass(2) { z = 3 };

                // Prints: MainMethod: a=10 (before PartialMethodCall)
                System.Console.WriteLine("MainMethod: a={0} (before PartialMethodCall)\n", a);

                // Partial Method can not return a value or have an out parameter
                // but they can have ref parameters
                myPartialClass.ThePartialMethod(ref a);

                // Prints: MainMethod: a=20 (after PartialMethodCall)
                System.Console.WriteLine("MainMethod: a={0} (after PartialMethodCall)\n", a);
            }
        }
    }