C # Several ways to judge an empty object (string, etc.)

Source: Internet
Author: User

(1) Learn about several keywords and objects related to empty types first

Null:

The keyword means that a null reference to any object is not referenced, it is the default value for all reference type variables, and only the reference variable type can be null before version 2.0, such as (string a=null) but after the C # 2.0 version, NET also introduces a nullable value type. But variables are defined in different ways, such as: int? A=null. See http://msdn.microsoft.com/zh-cn/library/1t3y8s4s.aspx in detail. Does not open memory addresses in memory and can be used to refer to whether they are available.

DBNULL:

is a unique class, meaning that there is only one instance of this type, that is, DBNull.Value. Used primarily to indicate when a field value in a database is empty (null). NET and can also be used to differentiate VT_NULL variables (associated with empty objects) and VT_EMPTY variables (associated with DBNull.Value instances) in COM Interop. See http://msdn.microsoft.com/zh-cn/library/system.dbnull%28v=vs.80%29.aspx in detail. It should have a fixed amount of memory.

String.Empty and "":

Both represent an empty string, and an empty string is a special string, so it is still a string instance, meaning it still has an explicit address assigned to it in memory.

Convert.isdbnull (): Returns an indication of whether the specified object is of type DBNull. That is used to determine whether the object is DBNull. Its return value is true or flase. Used primarily to determine the values associated with database operations.

(2) Various methods of use and efficiency

Object judgment: Using NULL can be used to determine whether any reference variable is NULL, and is the most basic object is null-judged, using Dbnull.vaule or Convert.isdbnull in database-related operations.

String judgment: String is also a reference type in C #, but it has a certain particularity. When we use string, we tend to confuse "string objects with variable values as empty" and "string objects with variable values as null characters." So often because of this understanding is not deep cause a lot of problems, because string is our development process use one of the most frequent type, for the method of determining the string, through the online data collation and its own practical experience is summarized as follows:

string str

1:string. IsNullOrEmpty = = str This method not only can judge "empty string variable" at one time, but also can judge "the variable of value is empty string", and also can make the code concise and Beautiful. The efficiency of judgment is also not low.

2:str. Length = = 0 This way, it is not recommended. This way of judging is the most efficient execution, but to use it you must ensure that the string is not NULL, and if it is null, the exception is reported.

3.str = = String. Empty or str = = "" These two ways, not recommended, he can only judge "value is empty string" string variable, and efficiency is low

4.STR = = NULL This method is not recommended, reason and 31 kind.

C # Several ways to judge an empty object (string, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.