Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy acquisition paradigm and C# Auto-Initializers #8245

Closed
JensMeinecke opened this issue Jan 29, 2016 · 1 comment
Closed

Lazy acquisition paradigm and C# Auto-Initializers #8245

JensMeinecke opened this issue Jan 29, 2016 · 1 comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue

Comments

@JensMeinecke
Copy link

In lots of classes we do something like this:

class MyClass
{
       private  OtherClass _otherValue;
       public OtherClass OtherValue
       { 
          get { return _otherValue?? (_otherValue = SomeExpensiveExpression());}
      }
}

In other words we have a read-only property with a backing field that we want to only evaluate once and keep in memory (classic lazy acquisition).

But now that we have Auto-Property Initializers, which automatically create a backing field, couldn't we simply extend this concept to a lazily acquired Auto-Property - in other words, only carry out the initialization when the property is first accessed.

I am not sure about the syntax, but how about something along these lines?

      public OtherClass OtherValue { get; } ??= SomeExpensiveExpression(); 

It would tidy up lots of classes immensely.

@alrz
Copy link
Member

alrz commented Jan 29, 2016

Dup of #3415, #4374 and a couple of others.

@gafter gafter added the Resolution-Duplicate The described behavior is tracked in another issue label Feb 8, 2016
@gafter gafter closed this as completed Mar 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

4 participants