Should we make some classes and methods sealed?
- MeteoricDragon
- Posts: 141
- Joined: Mon Feb 12, 2018 8:23 pm
Should we make some classes and methods sealed?
I was told that making a class or method sealed in C# can offer a performance benefit. Should we make some sealed which aren't intended to be inherited from?
- Interkarma
- Posts: 7247
- Joined: Sun Mar 22, 2015 1:51 am
Re: Should we make some classes and methods sealed?
The sealed feature is just to prevent other classes from inheriting, I don't believe it offers any tangible performance benefits.
I've heard this one before and I understand performance-wise it's no different to simply not declaring virtual methods if not required to be virtual. Heavy use of virtuals is where performance can be impacted, rather than whether a class can be inherited from or not. Otherwise it would be a rather bad language design as everything should be sealed by default.
I don't feel it's a valuable change, I'm sorry.
I've heard this one before and I understand performance-wise it's no different to simply not declaring virtual methods if not required to be virtual. Heavy use of virtuals is where performance can be impacted, rather than whether a class can be inherited from or not. Otherwise it would be a rather bad language design as everything should be sealed by default.
I don't feel it's a valuable change, I'm sorry.