简体   繁体   中英

Merge conditional expression C#

I need my code to use conditional expression, but not sure how to fix this correct.

Here is the "old" code:

private int PageNumber
{
    get
    {
        if (ViewState["PageNumber"] != null)
        {
            return (int)ViewState["PageNumber"];
        }
        return 0;
    }
    set
    {
        ViewState["PageNumber"] = value;
    }
}

Below is the "new" code. I have tried to modify the "old" code, but get "Merge conditional expression" in "get":

private int PageNumber
{
    get => ViewState["PageNumber"] != null ? (int) ViewState["PageNumber"] : 0;
    set => ViewState["PageNumber"] = value;
}

How can "get" use conditional expression?

Unfortunately, ReSharper does not propose a solution. But it likes the following more than your solution.

private int PageNumber
{
    get => (int)(ViewState["PageNumber"] ?? 0);
    set => ViewState["PageNumber"] = value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM