Methods and differences of empty string Initialization

Source: Internet
Author: User
Tags dotnet

(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.
String str2 = NULL. After this definition, a reference to the string class is defined. str2 does not point to any place. If it is not instantiated before use, an error is returned.

(2) "" And string. Empty

Both of these are empty strings, with a focus on the difference between string str1 = "" and string str2 = NULL. After this definition, str1 is an empty string, 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 is returned.

String. Empty does not allocate storage space
"" Allocate a bucket with an empty Length
Therefore, we generally use string. Empty

String. empty is a special value, which indicates that the string is empty. different from the value assigned to a string as "", although the string length is 0 and does not contain any character, it has a value. the difference between a 0-length string and a null string is that it allows you to check whether the string has been set or its initial value has been modified. one reason for the initial reputation value is: if the initial value is not known, a warning will appear in vWD.

 

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 = ""

The best way to judge whether the string is null is S. Length = 0!

 

(3) dbnull

Dbnull is a separate type in DOTNET. 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.

 

(4) convert. isdbnull ()

Convert. isdbnull () returns whether the specified object is Indicates whether the object is dbnull. The return value is true or flase.

 

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.