views:

102

answers:

4

I think sealed should be included in the list of access modifiers in C# language. Can somebody tell the reason why it has been excluded?

+9  A: 

It's not an access modifier, it's to do with whether a class can be inherited from or not...

Paddy
like `abstract`
abatishchev
a class, or a method, or a property
Abel
+4  A: 

Cause if you cannot derive from a class it doesn't mean you cannot access it.

Hamid Nazari
+4  A: 

An access modifier defines who can access the method or class, and when (i.e.: private: only class members, public: everyone else etc). Marking a method or a class as sealed means that it cannot be inherited. It says nothing about access per se.

Properly said: you still need to add an access modifier if you use the sealed keyword (unless the default access modifier suits you).

Your confusion may be about that both keywords seem to be about protection levels. This is kind of true, but we don't have a notion of protection modifier. The sealed keyword is called the sealed modifier, because it modifies a class or method to be sealed off. This is like a boolean switch: a class or method is either sealed or it is not, regardless of its access modifiers.

To add to the confusion, there exist sealed accessors, which means that derivation of an accessor (gettor/settor) is not allowed (C# standard 10.7.5).

Abel
A: 

All the following valid class definitions feature sealed classes but they all have different levels of access, so you can see that sealed isn't an access modifier and hence isn't listed as one by Microsoft:

public sealed class MyPublicClass
{
}

internal sealed class MyInternalClass
{
}

private sealed class MyPrivateClass
{
}

You have to trust that Microsoft do know a thing or two about the language they created :)

Dan Diplo
Anders Hejlsberg architected the language c#, and he came from Borland ;-)
Abel