C# Lazy Loaded Automatic Properties

前端 未结 12 2139
执笔经年
执笔经年 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 11:44

    Operator ??= is available using C# 8.0 and later, so you can now do it even more concise:

    private string _someVariable;
    
    public string SomeVariable => _someVariable ??= SomeClass.IOnlyWantToCallYouOnce();
    
    

提交回复
热议问题