Search code examples
c#.netc#-4.0private

Private const defined on class level and then used on an attribute, why this compile?


Have a look at the following code:

public class Person
{
    private const string MyConst = "SomeValue";

    [MyAttribute(MyConst)]
    public string Name {get;set;}
}

MyConst is defined as private and in my opinion it should not be accessible for MyAttribute. However this code compiles well.

Anyone knows the reason?


Solution

  • Private members are only accessible within the same class body as where they're declared. I guess it's accessible because you're declaring and using it in the same class.