CSharp - What is the output: instance constant


What is the output from the following code

using System;
class TestConstants
    public const int MYCONST = 100;

class Program
    static void Main(string[] args)
        TestConstants tc = new TestConstants();
        Console.WriteLine(" MYCONST is {0}", tc.MYCONST);


Click to view the answer

compile-time error


constants are implicitly static.

We cannot access them through an instance reference.

We should use the class name here. So, the following line of code will work fine:

Console.WriteLine(" MYCONST is {0}", TestConstants.MYCONST);

Related Topic