Thursday, 17 January 2019

What are the Restrictions in partial methods in c#

·       Partial methods are private by default.
·       You can not return any values using partial methods that mean it always void
·       You have to declare a partial method with signature and input parameters first just like we are declaring an abstract method in c# before implementation.
·       As a partial method is always private you can not use this directly with an instance. So to use this we have to use some other helper methods to call and implement.
·       Like partial classes multiple implementations are not allowed. You will get the compilation error

Severity
Code
Description
Project
File
Line
Suppression State
Error
CS0757
A partial method may not have multiple implementing declarations
Partial Classes


Active
·     
       A partial method should always be declared inside a partial class.

·       Try to avoid using partial methods as it will not add any performance to your application. Only use this when you are not sure to know what implementation logic need to be written and when dealing with the private methods.

·       Partial method declarations must begin with the contextual keyword partial and the method must return void.
·       Partial methods can have in or 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.



No comments:

Post a Comment