How to declare a class instance as a constant in C#?

前端 未结 7 1055
逝去的感伤
逝去的感伤 2020-12-16 09:46

I need to implement this:

static class MyStaticClass
{
    public const TimeSpan theTime = new TimeSpan(13, 0, 0);
    public static bool IsTooLate(DateTime          


        
7条回答
  •  别那么骄傲
    2020-12-16 10:12

    Using readonly instead of const can be initialized and not modified after that. Is that what you're looking for?

    Code example:

    static class MyStaticClass
    {
        public static readonly TimeSpan theTime;
        static MyStaticClass
        {
            theTime = new TimeSpan(13, 0, 0)
        }
    }
    

提交回复
热议问题