Comparison of Three Types of null characters and Comparison of Three Types of Characters

Source: Internet
Author: User

Comparison of Three Types of null characters and Comparison of Three Types of Characters

1. Differences between string. Empty and ""

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:

 

2. 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 performance comparison chart provided by Sunday in the comments can be referred to (the specific application is not clear and is for reference only ):

3. Differences between String. Empty and Null:

When you define only one str without allocating memory for it, use string str = null, however, before using it, you must assign a value for it, such as str = "xxxx", that is, initialize it.

String str = string. Empty is defined and initialized together. It is equivalent to string str = null; str = "";

For specific use, if you are sure to assign values to str before use, use string str = null; otherwise, use string str = string. Empty; otherwise, a null pointer exception may occur during program running.

4. Differences between null and DBNull

Null is an invalid object reference in. net.

DBNull is a class. DBNull. Value is its unique instance. It indicates the value in. net when the data in the database is NULL (<NULL>.

Null indicates that the pointing of an object is invalid, that is, the object is empty.

DBNull. Value indicates that the Value of an object in the database is null or not initialized. The DBNull. Value object is a valid object.

DBNull in DotNet is a separate type of System. DBNull. It has only one Value, DBNull. Value. This class cannot be inherited. DBNull directly inherits the Object, so DBNull is not a string, not an int, or DateTime...

But why does DBNull indicate a string, number, or date in the database? The reason is that the classes (such as DataRow) used by DotNet to store the data are 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, but it is possible to throw an exception in the subscript out of bounds.

DBNull implements IConvertible. However, except that ToString is normal, other ToXXX will throw an error that cannot be converted.

You can use Convert. IsDBNull to determine whether a value is DBNull. Note that Convert. IsDBNull (null) is false, that is, null and DBNull. Value are different.

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.