「C#入門」constを使って定数を利用するサンプル

サンプルコード

using System;

public class ConstTest
{
    class TestClass
    {
        public int x;
        public int y;
        public const int c1 = 5;
        public const int c2 = c1 + 5;

        public TestClass(int p1, int p2)
        {
            x = p1;
            y = p2;
        }
    }

    static void Main()
    {
        TestClass kk = new TestClass(11, 22);
        Console.WriteLine("x = {0}, y = {1}", kk.x, kk.y);
        Console.WriteLine("c1 = {0}, c2 = {1}",
                          TestClass.c1, TestClass.c2);
    }
}

実行結果:
x = 11, y = 22
c1 = 5, c2 = 10

C#

Posted by arkgame