All Questions
Tagged with lazy-initialization c#
97 questions
2
votes
1
answer
306
views
Naming convention for private properties with backing field in C#
Is there an official naming convention for private properties?
I use them for lazy initialization of expensive fields:
private string _veryExpensive;
private string VeryExpensive => _veryExpensive ?...
1
vote
1
answer
514
views
Am I using in a wrong way async lazy initialization?
I have a view model with 2 collections, that takes the data from database. For example countries and type of VAT.
I would like to defer the initialization of this collections until I will, becauase if ...
1
vote
0
answers
66
views
How can I implement a thread-safe lazy-initialized singleton in C# without using the `Lazy<T>` class? [duplicate]
I've been reading about the Singleton pattern in C# and how thread safety can be a concern, especially when it comes to lazy initialization. I'm aware of the Lazy<T> class which can be used to ...
0
votes
1
answer
331
views
Lazy tag is not working as supposed to be - Lazy is not lazy - initialized before used / called
I am intending to use lazy initialization with a .NET core 6 WPF application with the following.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
...
2
votes
3
answers
1k
views
Property initialization and 'this'
I was wandering if I could initialize a (reference type) property (when its value is null) using a reference to this keyword, but without using the constructor.
In some cases I do not want to use the ...
3
votes
1
answer
3k
views
Lazy property initialization not working in C#
I'm having trouble initializing properties within a class using the method described in this documentation.
Sample:
public class MyClass
{
private Lazy<string> _lazyString;
public ...
0
votes
0
answers
260
views
Problems with the implementation of ConcurrentDictionary and Lazy<T>
public class SystemBase
{
***private ConcurrentDictionary<string, ProfitTarget> dictTP = new ConcurrentDictionary<string, ProfitTarget>();***
private void DrawOrderLineTP(Order ...
0
votes
1
answer
2k
views
how can I Implement Lazy with Unity Dependency Injection
I have a class where I am injecting two service dependencies. I am using Unity container.
public UserService(ILazyInitialization<AClass> aCLass, ILazyInitialization<BClass> bClass)
{ ...
1
vote
0
answers
2k
views
Lazy Initialization (Lazy<T>) or Property with a Backing Field [duplicate]
According to Microsoft,
Lazy initialization of an object means that its creation is deferred
until it is first used. (For this topic, the terms lazy initialization
and lazy instantiation are ...
2
votes
0
answers
265
views
How to use Lazy<T>(Func<T> valueFactory) in a way that if init fails It should not use cached exception and should not run multiple threads for init? [duplicate]
How to use Lazy(Func<T> valueFactory) of C# in such a way that, if initialization fails, it should not use cached Exception and at the same time it should not run multiple threads for ...
1
vote
1
answer
516
views
Initialize Lazy<T> with a known Value
Question (TL;DR) in this constructor:
internal AnimalCollection(IReadOnlyList<Animal> animals, AnimalFlags aggregatedFlags)
{
AnimalList = animals;
_lazyAggregatedFlags = new Lazy<...
1
vote
1
answer
371
views
Update method on ConcurrentDictionary implemented with Lazy<T> does not work
As described in this blog article, I used the Lazy<T> class to make my ConcurrentDictionary thread-safe. I created a new class called LazyConcurrentDictionary<TKey, TValue> which stores a ...
0
votes
2
answers
516
views
Lazy/Enforced Initialization of Primitive Typed Property in C#
Often when coding in C# I like to use this pattern for a lazy getter property:
private string _myProp;
string MyProp => _myProp ?? (_myProp = getMyProp());
I think this is pretty standard C# ...
0
votes
0
answers
29
views
How should I understand the behavior of Lazy<T> on a web server?
I can't tell if or when it makes sense to use Lazy<T> on an web server. I understand how Lazy<T> works and why it makes sense in the basic case where an object may not be needed on my ...
0
votes
2
answers
678
views
Is delayed initialization good or bad? [closed]
I just found a class named HmeRevisionTracker that a colleague created. It merely contains a List<> of simple objects. CSimCharge, one of the most important classes in this project, contains an ...