Can't read a const in a class instance?

后端 未结 3 1505
忘了有多久
忘了有多久 2021-01-01 10:55

I was mildly surprised when the compiler complained about this:

public class UsefulClass
{
    public const String RatingName = @\"Ratings\\rating\";
}

publ         


        
3条回答
  •  耶瑟儿~
    2021-01-01 11:39

    Because const in c# are implicitly of static type. And As static members can be accessed only on class member and not on instance, const cannot too.

提交回复
热议问题