Достъпване на поле в Class
Здравейте, може ли някой да обясни тези 2 грешки
// Compile-time error: cannot access PI like a filed
// Compile-time error: Size is instance field (non-static) в този код:
public class ConstReadonlyModifiersTest
{
public const double PI = 3.1415926535897932385;
public readonly double size;
public ConstReadonlyModifiersTest(int size)
{
this.Size = size; // Cannot be further modified!
}
static void Main()
{
Console.WriteLine(PI);
Console.WriteLine(ConstReadonlyModifiersTest.PI);
ConstReadonlyModifiersTest instance =
new ConstReadonlyModifiersTest(5);
Console.WriteLine(instance.Size);
// Compile-time error: cannot access PI like a filed
Console.WriteLine(instance.PI);
// Compile-time error: Size is instance field (non-static)
Console.WriteLine(ConstReadonlyModifiersTest.Size);
// Compile-time error: cannot modify a constant
ConstAndReadOnlyExample.PI = 0;
// Compile-time error: cannot modify a readonly field
instance.Size = 0;
}
}