神刀安全网

Partial Class C#

In this article, we will discuss:

  • Partial Class and Partial types in C#
  • Advantages of Partial Class
  • Summary

Partial Class and Partial types in C# :

The class declaration can be partitioned into several partial class declarations.

  • Each of the partial class declarations contains the declarations of some of the class members.
  • The partial class declarations of a class can be in the same file or in different files.
  • Each partial declaration must be labeled as partial class,in contrast to the single keyword class.

Syntax:

partial class MyPartClass // Same class name as following { member1declaration member2declaration ... }   partial class MyPartClass // Same class name as preceding { member3declaration member4declaration ... } 

Example:

Partial Class C#

In the above diagram, left side represents a class฀declaration.In right side, same฀class declaration splited into two files using Partial.

Advantages of Partial Class

  • By Using Partial Classes, multiple developer can work on the same class easily.
  • Partial classes are mainly used by code generator to keep different concerns separate
  • you can also define Partial methods as well where a developer can simply define the method and the other developer can implement that.

Notes:

  • Use partial keyword in each part of partial class.
  • Name of each part of partial class should be the same but source file name for each part of partial class can be different.
  • All parts of a partial class should be in the same namespace.
  • Each part of a partial class should be in the same assembly or DLL, in other words you can’t create a partial class in source files of a different class library project.
  • Each part of a partial class has the same accessibility. (like private, public or protected)
  • If you inherit a class or interface on a partial class then it is inherited on all parts of a partial class.
  • If a part of a partial class is sealed then the entire class will be sealed.
  • If a part of partial class is abstract then the entire class will be considered an abstract class.

Summary:

In this article, we have discussed:

  • Partial Class and Partial types in C#
  • Advantages of Partial Class

Thanks for visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Partial Class C#

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址