How to declare constant in class with C#

1 Answer

0 votes
using System;

public class Program
{
    class Test {
        public int x;
        public const int C1 = 12;
        public const int C2 = C1 + 7;

        public Test(int _x) {
            x = _x;
        }
    }

    static void Main()
    {
        var o = new Test(99);
        
        Console.WriteLine(o.x);
        
        Console.WriteLine($"C1 = {Test.C1}, C2 = {Test.C2}");
    }
}



/*
run:

99
C1 = 12, C2 = 19

*/

 



answered Nov 23, 2020 by avibootz

Related questions

1 answer 143 views
143 views asked Nov 23, 2020 by avibootz
1 answer 161 views
1 answer 165 views
165 views asked Apr 28, 2016 by avibootz
1 answer 181 views
1 answer 135 views
135 views asked Jan 9, 2017 by avibootz
1 answer 133 views
1 answer 71 views
...