Some important concepts about partial classes in C#

Is it important to have at least two parts of the class to mark them as partials?

No, you can mark a single class as partial. It is not compulsory to have two parts of the class to use the concept of partial classes. In fact if you mark a single class as partial you are opening the way to extend this class by adding another partial class with the same name to your project and extending its functionality.

Can I have one partial class and one non-partial class with the same name?

No, the compiler will throw an error in this case. A partial and non-partial class with same name are treated as two different classes.

Can I put two functions or data members with the same name in two partial classes for the same class?

No, the rules which apply to normal classes also apply to partial classes. If you add two data members or functions with same name to different parts (partial classes) of the same class then the compiler will throw an error.

Can two partial parts of the same class be inherited from two different classes?

No, .net does not allow multiple inheritance. If you inherit two parts of the same class from different classes then the multiple inheritance rule will be violated and error will the thrown during compile time.

How many partial parts can I create for a class in C#?

You can create any number of partial classes for the same class in C#

Is it important to put partial classes in different code files?

No, you can have partials parts of the same class in the same code file also. It is not necessary to put them in different code files

Can I make a static class partial?

Yes, even static classes in C# can be made partials.

Do I have to put the static keyword in front of each partial part for the same class?

No, even if you mark one of the partial classes as static and do not mark other partial parts static the class will be treated as static by the compiler.

If I am inheriting a class from a parent class then do I have to inherit each partial part from that same class?

No, just inheriting one of the partial parts from a parent class is sufficient. You do not have to inherit each part from the same parent class.

Can I have one part of the partial class in one assembly and the other part in another assembly?

No, the all the partial parts of the same class should be in the same assembly and in the same namespace. They can be in different files, but not in different assemblies.

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp