blogs

miércoles, 9 de septiembre de 2015

Partial Classes and Methods

Partial Methods:
A partial class or struct may contain a partial method, one part of the class contains the signature of the method. An option implementation may be defined in the same part or another part.
A partial methods 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.

  • 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 and therefore they can not be virtual.
  • A partial method must be declared within  a partial class  or partial struct
  • The partial methods can be static, they can also be extension methods if they are part of a static class.
  • The partial methods can have any amount of parameters; These can be ref, but it is not out

 The result of the compiler will be:

  • The compiler does not find an implementation of the method PartialMethod1(), so that eliminate the method of the class PartialClass as if it had never existed
  • The compiler deleted the call to  Partial method(PartialMethod1()) that is in the Method  publicMethod(). if we partial method does not have defined a body,the compiler delete this method.
  • PartialMethod2() has defined a body so that this method is present in this class


Partial Classes:
When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time


  • The code can be added to the class without having to recreate the source file.
  • To split a class definition,use the partial keyword modifier.
  • The partial keyword indicates that other parts of the class,struct or interface can be defined in the namespace,All the parts must use the partial keyword,they must have the same accessibility,.
  • If any part is declared abstract, then the whole type is considered abstract, if any part is declared sealed,then the whole type is considered sealed.
  • The partial modifier is not available on delegate or enumeration declarations


Utility of partial methods and classes
It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

Restrictions 


  • All partial-type must be defined in the same assembly and the same module.
  • The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
  • All partial-type definitions meant to be parts of the same type must be modified with partial
  • The partial modifier can only appear immediately before the keywords class, struct, or interface


For example








No hay comentarios:

Publicar un comentario