Should C# have a lazy keyword to make lazy initialization easier?
E.g.
public lazy string LazyInitializeString = GetStringFromDatabase();
instead of
private string _backingField;
public string LazyInitializeString
{
get
{
if (_backingField == null)
_backingField = GetStringFromDatabase();
return _backingField;
}
}
As a middle-level language, C combines the features of both high-level and low-level languages. It can be used for low-level programming, such as scripting for drivers and kernels and it also supports functions of high-level programming languages, such as scripting for software applications etc.
The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.
C exists everywhere in the modern world. A lot of applications, including Microsoft Windows, run on C. Even Python, one of the most popular languages, was built on C. Modern applications add new features implemented using high-level languages, but a lot of their existing functionalities use C.
There is no need to learn C before learning C++. They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own. Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.
I don't know about a keyword but it now has a System.Lazy<T>
type.
member
.lambda expression
or a method
to provide a value.public class ClassWithLazyMember
{
Lazy<String> lazySource;
public String LazyValue
{
get
{
if (lazySource == null)
{
lazySource = new Lazy<String>(GetStringFromDatabase);
// Same as lazySource = new Lazy<String>(() => "Hello, Lazy World!");
// or lazySource = new Lazy<String>(() => GetStringFromDatabase());
}
return lazySource.Value;
}
}
public String GetStringFromDatabase()
{
return "Hello, Lazy World!";
}
}
var obj = new ClassWithLazyMember();
MessageBox.Show(obj.LazyValue); // Calls GetStringFromDatabase()
MessageBox.Show(obj.LazyValue); // Does not call GetStringFromDatabase()
In above Test code, GetStringFromDatabase()
gets called only once. I think that is exactly what you want.
After having comments from @dthorpe and @Joe, all I can say is following is the shortest it can be:
public class ClassWithLazyMember
{
Lazy<String> lazySource;
public String LazyValue { get { return lazySource.Value; } }
public ClassWithLazyMember()
{
lazySource = new Lazy<String>(GetStringFromDatabase);
}
public String GetStringFromDatabase()
{
return "Hello, Lazy World!";
}
}
Because following does not compile:
public Lazy<String> LazyInitializeString = new Lazy<String>(() =>
{
return GetStringFromDatabase();
});
And that property is type of Lazy<String>
not String
. You you always need to access it's value using LazyInitializeString.Value
.
And, I am open for suggestions on how to make it shorter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With