Checking if a datatable is null

后端 未结 7 1907
南旧
南旧 2021-02-19 22:02

The following code is what I\'ve been using to retrieve user information from a sql database.

            string userName = LoginUser.UserName;
            strin         


        
相关标签:
7条回答
  • 2021-02-19 22:35

    As of C# 6.0 you can use the Null conditional operator ?. (or ?[] for arrays).

    The null conditional operator simplifies the statement to:

    if (dt?.Rows?.Count > 0)
    

    This returns false when:

    1. the data table dt is null
    2. data table Rows dt.Rows is null
    3. the number of rows dt.Rows.Count is 0

    Using the null conditional operator you can avoid manually checking both the data table and count properties, eg if (dt != null && dt.Rows.Count > 0)

    0 讨论(0)
提交回复
热议问题