Partial Method

Partial methods can only be used in conjunction with partial classes.
A partial method has its signature defined in one part of a partial class and its implementation defined in another part of the partial class.
If an implementation is not provided then the program will still compile and the redundant signature is automatically ignored.


Applies to classes, structures, interfaces
A partial modifier can be used to split the definition of a class, struct or interface across multiple files.
Each file contains a different component of the definition
Both components are combined when the application is compiled
If any of the components are declared abstract or sealed then the entire type is abstract or sealed respectively


Signatures in both parts must be identical The method must return void The only access modifier that you can use is private.


    partial class A 
    {
      //implicitly private
        partial void DoSomething(string s);
    }

    // This part can be in a separate file.
    partial class A
    {
        partial void DoSomething(String s)
        {
            Console.WriteLine("Something happened: {0}", s);
        }
    }


© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext