Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of a sealed partial class?

In C#, what is the significance of a sealed partial class?

A Sealed class is a class that cannot be inherited.

A partial class is a class that can be split between 2 or more source files.

So why does a "sealed partial class have any significance?

like image 727
xarzu Avatar asked Sep 15 '25 22:09

xarzu


2 Answers

A partial class is basically just syntactic sugar, you can define a class in two different files, in the same assembly. This is useful when part of your class is generated, for example using the Entity Framework DB first approach. It has nothing to do with inheritance.

A sealed class is a class that cannot be inherited. You would make a class sealed if inheriting from it could potentially break it, but you need it to be accessable outside of your own code.

There is no relation between the two keywords. A sealed partial class is simply a class, that might be defined in more files, and cannot be inherited from.

like image 52
Robert Avatar answered Sep 17 '25 12:09

Robert


Partial class allows us to write a class across multiple files in a project. Partial indicates that the parts of the class can be defined in the namespace and all the parts must be used with the partial keyword. if any part is declared as sealed then the whole type is considered sealed.

like image 45
karmakula Avatar answered Sep 17 '25 12:09

karmakula