.NET

C#: Lazy is not constrained to static contexts, instance field initializers are: via Stack Overflow

Lazy<T> is not constrained to static contexts.

Instance field initialisers cannot use instance references (but can use static references) as they run outside of the constructor.Though there are arguments for instance field initializers too., I think this is a good reason to initialize fields inside the constructor: there you do have access to instance references (but should not call virtual instance methods or properties) which leads to another reason: consistency as field initializers run in the opposite hierarchy order as constructors (incidentally causing this virtual method restriction).

Boy, that was a long sentence

Related Articles

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button