Partial Method

Added in .NET 3.5
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.


A partial method is a method whose signature is declared in one class but its implementation is in a different class.
There are several restrictions though:
partial methods must have a return type of void
partial methods cannot accept out parameters
partial methods cannot be extern
partial methods cannot be virtual
partial methods are implicitely private


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)
        {
            System.Console.WriteLine("Something happened: {0}", s);
        }
    }


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