This compiles well for me - However other people on a different thread are saying that protected classes cannot be declared in c# at top level
Is that the case?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
protected class CsvReader
{
}
}
It doesn't compile for me with either VS2008SP1 or VS2010 (using csc.exe from the command line in both cases).
Which compiler are you using?
From section 3.5.1 of the C# specification:
- Types declared in compilation units or namespaces can have
public
orinternal
declared accessibility and default tointernal
declared accessibility.- Class members can have any of the five kinds of declared accessibility and default to
private
declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have onlypublic
orinternal
declared accessibility.)
Consider: what would protected
even mean on a top-level class. On a class member, it means that the member can be accessed by derived classes. But there's no such thing as a derived namespace, so what would protected
even refer to?
I don't know if it compiles or not, but ask yourself this: what would this mean? It would seem to indicate that CsvReader
could only be accessed from within the ConsoleApplication1
namespace, or any derived namespaces (?) ; but obviously this is nonsense. That's why it's not permitted in the spec. (At least that would be my explanation.)
Classes cannot be declared as protected at top level becuase then no code would be able to access it (unless inherited but then you should use abstract classes). Why create code that cannot be accessed?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.