Friday 11 April 2014

Partial (Method)

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. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not. If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods:
  • Signatures in both parts of the partial type must match.
  • The method must return void.
  • No access modifiers are allowed. Partial methods are implicitly private.
The first impression from the name partial method will be a method with multiple implementations but let me make it clear that partial methods have only one implementation or no implementation at all. As a developer, it becomes important to learn how to implement partial methods and know where to expect them if you have to maintain code. To keep it simple, partial keyword before a method is a way to notify the compiler that it is not mandatory for this method to have an implementation.
A partial method is like a usual method in a class except that the user may or may not implement it. A partial method gets executed only when it has an implementation. What is the use of a method that does not have an implementation? Such methods can act as hooks for plugging in code, for instance take a code generation tool that generates a class. It can have partial methods, the user of the class can implement them or just leave them. If the user leaves them, the compiler does not include them in the final code thus improving the overall performance. If the user writes some implementation code for them, then they act as normal methods.

Partial methods are especially useful as a way to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.
A partial method declaration consists of two parts: the definition, and the implementation. These may be in separate parts of a partial class, or in the same part. If there is no implementation declaration, then the compiler optimizes away both the defining declaration and all calls to the method.
// Definition in file1.cs
partial void onNameChanged();

// Implementation in file2.cs
partial void onNameChanged()
{
  // method body
}
  • Partial method declarations must begin with the contextual keyword partial and the method must return void.
  • Partial methods can have ref but not out parameters.
  • Partial methods are implicitly private, and therefore they cannot be virtual.
  • Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.
  • Partial methods can have static and unsafe modifiers.
  • Partial methods can be generic. Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one. Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.
  • You can make a delegate to a partial method that has been defined and implemented, but not to a partial method that has only been defined.
partial method is a special method that exist with in a partial class or struct. One part of the partial class or struct have only partial method declaration means signature and another part of the same partial class or struct may have implementation for that partial method. If the implementation is not provided for declared partial method, the method and all calls to that partial methods will be removed at compile time. For more about Partial Class, Interface or Struct refer the article Partial Class, Interface or Struct in C Sharp with example

Why partial methods required ?

Partial methods are particularly helpful for customizing auto generated code by the tool. Whenever the tool generate the code then tool may decalare some partial method and implementation of these methods is decided by the developers.
If you are using entity framework for making DAL then you have seen that the Visual Studio make a partial method OnContextCreated() as shown below. Now the implementation of it depends on you whether you want to use it or not.
  1. public partial class DALEntities : ObjectContext
  2. {
  3. #region Constructors
  4. // Constructors for DALentities
  5. #endregion
  6. #region Partial Methods
  7. partial void OnContextCreated();
  8. #endregion
  9. }
  10. // This part can be put in the separate file
  11. public partial class DALEntities : ObjectContext
  12. {
  13. partial void OnContextCreated()
  14. {
  15. // put method implementation code
  16. Debug.WriteLine("OnContextCreated partial method");
  17. }
  18. }

Key points about partial method

  1. Partial methods can be declared or defined with in the partial class or struct.
  2. Partial methods are implicitly private and declarations must have partial keyword.
  3. Partial methods must return void.
  4. Partial methods implementation is optional.
  5. Partial methods can be static and unsafe and generic.
  6. Partial methods can have ref parameters but not out parameters since these can't return value.
  7. You cannot make a delegate to a partial method.
  8. The signatures of partial method will be same in both parts of the partial class or struct.
  1. partial class Example
  2. {
  3. partial void ExampleMethod(string s);
  4. }
  5. // This part can be put in the separate file
  6. partial class Example { //Implement the method
  7. partial void ExampleMethod(String s)
  8. {
  9. Console.WriteLine("Your string: {0}", s);
  10. }
  11. }
The following example shows a partial method defined in two parts of a partial class:
namespace PM
{
    partial class A
    {
        partial void OnSomethingHappened(string s);
    }

    // This part can be in a separate file. 
    partial class A
    {
        // Comment out this method and the program 
        // will still compile. 
        partial void OnSomethingHappened(String s)
        {
            Console.WriteLine("Something happened: {0}", s);
        }
    }
}

No comments:

Post a Comment