After a little research I’ve determined that the only access modifiers which you can apply to classes are:
- public – available in any assembly
- internal – available only in the current assembly
but the error message below seems to imply that if a class is not defined in a namespace that it could be defined as private, protected, or protected internal.
Are public and internal the only class modifiers you can use on class or are there more?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test2343434
{
class Program
{
static void Main(string[] args)
{
Tools.ToolManager toolManager = new Tools.ToolManager();
Console.ReadLine();
}
}
}
namespace Tools
{
//error: Elements defined in a namespace cannot be explicitly
//declared as private, protected, or protected internal
private class ToolManager
{
public ToolManager()
{
Console.WriteLine("inside tool manager");
}
}
}
A nested type can be declared private, protected, or protected internal. You can still declare nested types as public or internal of course – it’s just that you can only declare them with the above access modifiers when they’re nested:
Note that you can’t declare a type nested in a
structto beprotectedorprotected internalbecause it doesn’t make any sense to do so 🙂C# doesn’t allow types to be nested in interfaces (unfortunately, IMO – it would be useful for code contracts).
All of this is true for other nested types (enums, structs, interfaces, delegates) too.