Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should C# have a lazy key word

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;
        }
    }
like image 614
Jonathan Parker Avatar asked Dec 02 '10 06:12

Jonathan Parker


People also ask

When should C be used?

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.

Should we use C?

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.

Should C still be used?

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.

Should I learn C before 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.


1 Answers

I don't know about a keyword but it now has a System.Lazy<T> type.

  • It is officially part of .Net Framework 4.0.
  • It allows lazy loading of a value for a member.
  • It supports a lambda expression or a method to provide a value.

Example:

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!";
    }
}

Test:

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.

Edit:

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.

like image 147
decyclone Avatar answered Nov 02 '22 14:11

decyclone