C# Lazy Loaded Automatic Properties

前端 未结 12 2183
执笔经年
执笔经年 2020-12-04 11:08

In C#,

Is there a way to turn an automatic property into a lazy loaded automatic property with a specified default value?

Essentially, I am trying to turn th

12条回答
  •  心在旅途
    2020-12-04 12:02

    There is a new feature in C#6 called Expression Bodied Auto-Properties, which allows you to write it a bit cleaner:

    public class SomeClass
    { 
       private Lazy _someVariable = new Lazy(SomeClass.IOnlyWantToCallYouOnce);
    
       public string SomeVariable 
       {
          get { return _someVariable.Value; }
       }
    }
    

    Can now be written as:

    public class SomeClass
    {
       private Lazy _someVariable = new Lazy(SomeClass.IOnlyWantToCallYouOnce);
    
       public string SomeVariable => _someVariable.Value;
    }
    

提交回复
热议问题