The difference between null, "", Dbnull,string.empty,convert.isdbnull () in C #

Source: Internet
Author: User
Tags dotnet

The judgment of NULL in C # is more troublesome, not as simple as in VB6, these various null values of judgment and understanding for unfamiliar people, may be very troublesome, now I in the use of the process of a little experience and shared with you.

(1) Null

The NULL keyword is a literal value that represents a null reference that does not reference any object. NULL is the default value for a reference type variable. Then only the variable of the reference type can be null, if int i=null, then it is not possible, because int is a value type.

(2) DBNULL

DBNULL is a single type in dotnet, and the class can only have a unique instance, DBNULL. Value,dbnull's only function is to represent a string, number, or date in a database, and why it can be expressed because the class that stores the data dotnet (DataRow, etc.) is stored in the form of object. For a DataRow, the value returned by its row[column] is never null, or it is the value of the type of column that is specific. Or it's DBNull. So Row[column]. The ToString () is never written in ToString where NullReferenceException occurs. DBNull implements the IConvertible. However, except that ToString is normal, other toxxx will throw an error that cannot be converted.

(3) "" and String.Empty

Both of these represent empty strings, one of which focuses on the difference between string str1= "" and string str2=null, so that after the definition, str1 is an empty string, and the empty string is a special string, except that the value of the string is empty and there is an exact point in memory , string str2=null, when defined, simply defines a reference to a string class, and str2 does not point to any place, and if it is not instantiated before it is used, it will be held wrong.

(4) Convert.isdbnull ()

Convert.isdbnull () returns an indication of whether the specified object is a DBNull type, which is used to determine whether the object is DBNull. Its return value is true or flase.

Well, said so much, do not know to say that there is no, in fact, these broad sense of "empty value" after understanding, or there is a great difference, or even no relationship at all. is entirely two concepts.

The difference between null, "", Dbnull,string.empty,convert.isdbnull () in C #

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.