Skip to content

[API Proposal]: Add overload of Lazy<T> constructor which allows to turn off exception caching behaviour #114147

Open
@Dzliera

Description

@Dzliera

Background and motivation

Lazy<T> currency always caches exceptions When LazyThreadSafetyMode is ExecutionAndPublication or None and factory method is used. This often is not desired behavious as factory method often can throw exception caused by transient error.

API Proposal

Add overload with additional bool or enum parameter, which allows to explicitly indicate exception caching behaviour:

 public Lazy(Func<T> valueFactory, LazyThreadSafetyMode mode, bool cacheException)

API Usage

public class ExampleClass
{
  public DatabaseConnection DatabaseConnection => _databaseConnectionLazy .Value;

  private DatabaseConnection GetConnection(){}
  
  private Lazy<DatabaseConnection> _databaseConnectionLazy = new Lazy<DatabaseConnection> 
    (GetConnection, LazyThreadSafetyMode.ExecutionAndPublication, false);
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.RuntimeuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions