.net | Dmytro Shteflyuk's Home https://kpumuk.info In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming. Mon, 07 Sep 2015 23:48:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Coalesce ?? operator in C# 2.0 https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/ https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/#comments Mon, 14 Jan 2008 19:18:27 +0000 http://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/ Operator ??, that was introduced in the .NET 2.0, takes first place in my top used C# idioms list a long time, but unfortunately it is rarely used in projects I’ve participated. Therefore these snippets could be found in production code very often: 12345public string Caption {     get { return ViewState["Caption"] != null […]

The post Coalesce ?? operator in C# 2.0 first appeared on Dmytro Shteflyuk's Home.]]>
Operator ??, that was introduced in the .NET 2.0, takes first place in my top used C# idioms list a long time, but unfortunately it is rarely used in projects I’ve participated. Therefore these snippets could be found in production code very often:

1
2
3
4
5
public string Caption
{
    get { return ViewState["Caption"] != null ? (string) ViewState["Caption"] : ""; }
    set { ViewState["Caption"] = value; }
}

Or even:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public string VisibleStatistic
{
    get
    {
        string retval = (string) ViewState["VisibleStatistic"];
        return retval == null ? "" : retval;
    }
    set
    {
        ViewState["VisibleStatistic"] = value;
    }
}

public string SelectedCategoryName
{
    get
    {
        object retval = ViewState["SelectedCategoryName"];
        if (retval != null)
            return (string) retval;
        return String.Empty;
    }
    set
    {
        ViewState["SelectedCategoryName"] = value;
    }
}

Almost 50% of this could be easily removed, and you will get beautiful and clear code, just right after you will understand what the hell is operator ??. Here is information from MSDN:

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

Easy and clean explanation. Let’s try to rewrite all these examples using this operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public string Caption
{
    get { return (string) ViewState["Caption"] ?? String.Empty; }
    set { ViewState["Caption"] = value; }
}

public string VisibleStatistic
{
    get { return (string) ViewState["VisibleStatistic"] ?? String.Empty; }
    set { ViewState["VisibleStatistic"] = value; }
}

public string SelectedCategoryName
{
    get { return (string) ViewState["SelectedCategoryName"] ?? String.Empty; }
    set { ViewState["SelectedCategoryName"] = value; }
}

In case of value-types this approach will not work: you will get NullReferenceException. But do not worry — in this case we can use Nullable-types. Before:

1
2
3
4
5
public int FirstWidth
{
    get { return ViewState["FirstWidth"] != null ? (int) ViewState["FirstWidth"] : 0; }
    set { ViewState["FirstWidth"] = value; }
}

After:

1
2
3
4
5
public int FirstWidth
{
    get { return (int?) ViewState["FirstWidth"] ?? 0; }
    set { ViewState["FirstWidth"] = value; }
}

When you are using ?? operator, expression will be evaluated in the left to right order, so you can use something like this:

1
string name = FirstName ?? LastName ?? "Anonymous";

Of course, it looks more readably than the:

1
2
3
4
5
6
7
string name;
if (FirstName != null)
    name = FirstName;
else if (LastName != null)
    name = LastName;
else
    name = "Anonymous";

And a fortiori than:

1
2
3
string name = FirstName != null
        ? FirstName
        : (LastName != null ? LastName : "Anonymous");

Add it to your armoury!

The post Coalesce ?? operator in C# 2.0 first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/asp-net/coalesce-operator-in-c-sharp-2-0/feed/ 18