代码如下:
using System;
using System.Collections.Generic;
using System.Text;namespace test_Readonly
{
    class RightNow
    {
        static RightNow()
        {
            System.DateTime dt = System.DateTime.Now;
            Year = dt.Year;
            Month = dt.Month;
            Date = dt.Day;
            Hour = dt.Hour;
            Minute = dt.Minute;
            Second = dt.Second;
        }
        public static readonly int Year;
        public static readonly int Month;
        public static readonly int Date;
        public static readonly int Hour;
        public static readonly int Minute;
        public static readonly int Second;
    }    class Tester
    {
        static void Main()
        {        
            System.Console.WriteLine("This year: {0}", RightNow.Year.ToString());
        }
    }
}
以下这段代码是叫什么呢?
        static RightNow()
        {
            System.DateTime dt = System.DateTime.Now;
            Year = dt.Year;
            Month = dt.Month;
            Date = dt.Day;
            Hour = dt.Hour;
            Minute = dt.Minute;
            Second = dt.Second;
        }
为什么将它设为static??

解决方案 »

  1.   

    这段代码 实现个功能是  获取系统当前时间.
     Year = dt.Year; //年
                Month = dt.Month; //月
                Date = dt.Day; //日
                Hour = dt.Hour; //时
                Minute = dt.Minute; //份
                Second = dt.Second; //秒
    至于为什么要设置成静态方法...我建议你去上 百度 或者 vs 的 msdn 文档查看一下... 因为它又好多中用法...
     
      

  2.   

    静态方法,设为静态主要是让它常驻内存,供RightNow类的其它方法调用。