C # 's const- це те саме, що і Java final, за винятком того, що це завжди завжди static. На мою думку, constзмінна не дуже потрібна static, але якщо вам потрібно отримати доступ до constзмінної static, не можна зробити це:
class MyClass
{
private const int myLowercase_Private_Const_Int = 0;
public const int MyUppercase_Public_Const_Int = 0;
/*
You can have the `private const int` lowercase
and the `public int` Uppercase:
*/
public int MyLowercase_Private_Const_Int
{
get
{
return MyClass.myLowercase_Private_Const_Int;
}
}
/*
Or you can have the `public const int` uppercase
and the `public int` slighly altered
(i.e. an underscore preceding the name):
*/
public int _MyUppercase_Public_Const_Int
{
get
{
return MyClass.MyUppercase_Public_Const_Int;
}
}
/*
Or you can have the `public const int` uppercase
and get the `public int` with a 'Get' method:
*/
public int Get_MyUppercase_Public_Const_Int()
{
return MyClass.MyUppercase_Public_Const_Int;
}
}
Ну, тепер я розумію, що це запитання було задано 4 роки тому, але оскільки я вклав у цю відповідь дві години роботи, що складаються з спроб різного роду відповідей та форматування коду, я все одно публікую його. :)
Але, для запису, я все одно відчуваю себе якось дурним.