7 Replies
I was thinking about using Lazy but that doesn't seem to clean thing up
Is the
bum
approach better?You're trying to implement lazy behavior. So using
Lazy
seems generally like a good approach.
Do you require thread safety? Do you not care about the extra allocation? Then Lazy
will probably fit the bill. Nothing wrong with doing it manually either if your specific usecase doesn't suffer for it.
There's no "better" approach to be recommended without more detail on your specific usecase imo
In fact, in your specific sample code, I'd go for a manual implementation since the null check and factory are cheap plus the factory is side effect free and so concurrent execution is a non-issue.putting the logic of initializing bum into the constructor just seems weird
so use a field initializer
private readonly Lazy<int> _lazySum = new(()=>X+Y);
Or extract the factory delegate into a method.field initializers cannot reference non-static properties
right.
Constructor it is, then :)
Thanks