C # null value judgment)

Source: Internet
Author: User

(1) NULL

The null keyword is a text value that does not reference any object's null reference. Null is the default value of the reference type variable. Then, only the referenced variable can be NULL. If int I = null, it is not possible because Int Is of the value type.

 

(2) "" And String. Empty

Both are empty strings. But "" theoretically re-opens the memory space, and String. Empty points to one. But the optimizer will optimize it!

String. Empty does not allocate a bucket. "" It is used to allocate a bucket with an Empty length. Therefore, string. empty is generally used. For future cross-platform use, string. Empty is used. In C #, "" And string. Empty Can be used interchangeably. For example:

String s = "";

String s2 = string. Empty;

 

If (s = string. Empty ){

//

} If statement is true

 

Several methods for determining as a Null String, in the order of performance from high to low:

 

S. Length = 0 is better than s = string. Empty is better than s = ""

 

 

Note:

 

1. The difference between string str1 = "" and string str2 = null. Str1 is an empty string, and a null string is a special string, except that the value of this string is null and has an accurate point in memory. string str2 = null, after this definition, only a string class reference is defined. str2 does not point to any place. If it is not instantiated before use, an error will be reported.

 

2. Check whether String. IsNullOrEmpty (param) is null or null in net 2.0.

 

If the Request. QueryString flag does not exist, NULL is returned. You can call all methods of the string class on an empty string. However, null is not allowed. You cannot call a method on null.

 

(3) DBNULL

 

DBNull is a separate type in DotNet, which is used to indicate that no known value exists (usually in database applications ). This class can only have unique instances, DBNULL. value, DBNull is used only to indicate the strings, numbers, or dates in the database. Why does it indicate the reason for the classes (such as DataRow) That DotNet stores the data) all store data in the form of objects. For DataRow, the value returned by its row [column] is never null, or it is a specific value of the column type. Or DBNull. Therefore, the row [column]. ToString () Statement will never cause NullReferenceException in ToString. DBNull implements IConvertible. However, except that ToString is normal, other ToXXX will throw an error that cannot be converted.

You can pass the Value retrieved from the database field to DBNull. Value. Equals to determine whether the field Value is DBNull.

(4) Convert. IsDBNull ()

 

Convert. IsDBNull () indicates whether the specified object is of the DBNull type, that is, whether the object is DBNULL. The return value is True or Flase.

 

PS: This article has been collected online by individuals and added to problems encountered during development. Here I want to clarify that do not conduct personal attacks. The purpose of this article is 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.