The Judgment of C # Hollow value

Source: Internet
Author: User

(1) Null

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

(2) "" and String.Empty

Both of these represent empty strings. It's just "theoretically reopening the memory space, and the String.Empty pointing to one." But the optimizer will be optimized!

String. Empty does not allocate storage space, "" allocates a space with an empty length, so it is usually string. Empty, in order to cross the platform later, or with String.Empty. In C #, most of the cases "" and string. Empty can be used interchangeably. Like what:

string s = "";

string s2 = string.Empty;


if (s == string.Empty) {

 //

}if语句成立

Several ways to determine an empty string are in the order of performance from highest to lowest:

s.Length == 0 优于 s == string.Empty 优于 s == ""

Attention:

1.string str1= The difference between "" and string str2=null. STR1 is an empty string, the empty string is a special string, except that the value of this string is empty, in memory there is an accurate point, string str2=null, so defined, just defines a string class reference, STR2 does not point to any place, If not instantiated before use, will be an error.

2. Available String.IsNullOrEmpty (param) in NET 2.0 detects null or null values.

Null is returned when the identity of the request.querystring does not exist, and all methods of the string class can be called on an empty string, but Null is not possible and the method cannot be invoked on null.

(3) DBNULL

DBNull is a separate type in dotnet that indicates that a known value is not present (typically in a database application). The class can only have unique instances, DBNULL. Value, DBNull's only function is to represent a string, a number, or a date in a database, and why it can be represented as a class (DataRow, etc.) that stores the data in the form of an object. dotnet For DataRow, its row[column] returns a value that is never null, or is the value of a type that is specific to column. Or it's DBNull. So Row[column]. ToString () This writing will never happen to NullReferenceException in ToString. DBNull realized the IConvertible. However, except that ToString is normal, other toxxx will throw errors that cannot be converted.

You can determine whether the field value is a DBNull value by passing the value retrieved from the database field to the DBNull.Value.Equals method

(4) Convert.isdbnull ()

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

PS: This article through personal online collection and collation, and to join the development of the problems encountered in this clarification please do not do personal attacks, the purpose of this article is just to share

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.