你真的了解const readonly吗

屁话不多说,直接上code

 1 public class Program
 2     {
 3         static readonly int A = B + 10;
 4         static readonly int B = 10;
 5         public static void Main(string[] args)
 6         {
 7             Console.WriteLine("A is {0},B is {1} ", A, B);
 8             Console.ReadLine();
 9         }
10     }

结果 是多少呢?=》 A is 10,B is 10 如果你知道,OK不用多看了,如果你想的是A is 20,B is 10,那么接着看

表态初始化A的时候此时B为动态常量,B此时还是0。

static readonly int A = B + 10;
        const int B = 10;
        public static void Main(string[] args)
        {
            Console.WriteLine("A is {0},B is {1} ", A, B);
            Console.ReadLine();
        }

 A is 20,B is 10 这个就是静态常量 相当于下面的代码

 static readonly int A = 10 + 10;
        const int B = 10;
        public static void Main(string[] args)
        {
            Console.WriteLine("A is {0},B is {1} ", A, 10);
            Console.ReadLine();
        }

好久没注意,尽量当时犯错了下,故记录。

 

上一篇:input 框变成不可编辑的。


下一篇:@Transactional(readOnly=true)引发的坑