Thursday, December 6, 2012

Why to use partial class or Interface or Struct in C# ?


Partial Class , Interface and Struct was introduced in C#.net 2.0 framework.

Lets see why to use this Partial Class or Interface or struct.

First main point it they  allow more than one developers to work simultaneously on the same class, struct or interface.
   
Second is Partial class are particularly helpful for customizing auto generated code by the IDE. Whenever the IDE generate the code then tool may define some partial class, interface, methods and further customization of partial class, interface is done by the developers without messing with the system generated code.


Now lets see main key points for using partial class or Interface or Struct in C# .



  •       During code compilation, all the parts should be available to form the final class, interface or           struct.
  •       Any member declared in the one part will be available to all other parts.
  •       If any part has Inheritance, then it applies to the entire class.
  •       Different parts of a class or struct may inherit from different interfaces.
  •       If any part is declared abstract, then the whole class, interface or struct is considered abstract.
  •       If any part is declared sealed, then the whole whole class, interface or struct is considered sealed.


Partial Class, Interface or Struct Example

Partial Class :-
 
class NameSpace
{
 partial class MyPartialClass
 {
 void PartialClass() {  }
 }
 partial class MyPartialClass
 {
 void PartialClass2() {  }
 }
}

Interface  :-

partial interface InterfaceExample
{
 void InterfaceTest();
}
 partial interface InterfaceExample
{
 void InterfaceTest2();
}

Struct  :-

partial struct StructExample
{
 void StructTest()
{
 }
}
 partial struct StructExample
{
void StructTest2()
{

}
}



No comments:

Post a Comment