In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the differences between DBNull.Value,null,String.Empty in asp.net". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the differences between DBNull.Value,null,String.Empty in asp.net"?
First, "null" is in C # (VB.NET is Nothing), indicating what happens when a reference object variable "does not reference" any entity (the typical symptom is that an exception such as NullException is thrown if a method of this variable is called).
String.Empty is a static public variable that indicates that a String variable does not contain any characters (equivalent to ""). However, in terms of performance, using String.Empty is better than declaring a ""-- obviously, the former is a static variable of the String class, and it produces only one instance anyway, while the latter can be used several times.
The copy code is as follows:
String str= ""
/ / "": indicates that the data reference storage area is allocated in the stack, the actual data storage area is created in the heap, the reference address is assigned to the variable, and an empty storage space is allocated in the heap.
String str=string.Empty
/ / string.Empty: indicates that a data reference storage area is allocated in the stack, an area is created in the heap for the actual data storage, and the reference address is assigned to the variable, but the storage space created in the heap is not allocated or the data is not stored.
As for DBNull.Value, it is also a static attribute. It is only used for the "null value" comparison of the database (for example, when reading data using DataReader, or when comparing a row or column of data in DataTable). Because a field in SQL is Null only means that the field "does not have any value", not "no reference" in C#. So pay attention to these issues:
1) if you use DataReader to perform ExecuteScalar, if you are not sure whether you will get the data, you must use null to judge (because of a null reference); if you are sure to read at least one piece of data, but are not sure whether the data is empty, you can use DBNull.Value for numerical judgment.
2) follow 1, if a field is convinced that there is no data, it is equivalent to no characters, which is equivalent to String.Empty and "", so it can be judged by String.Empty or "" (important conclusion: DbNull.Value=String.Empty= ").
3) in addition, if you assign a DataColumn of type string (such as null), even so, it is virtually impossible to store a null in DataTable (to correspond to the actual value of SQL), which will be converted to String.Empty or "". The judgment method is the same as the "important conclusion".
To put it simply:
String str = ""
I'll give you a blank piece of paper.
String str = null
Not even a blank piece of paper.
String.Empty is the equivalent of ""
Generally used for initialization of strings
For example:
The copy code is as follows:
String a
Console.WriteLine (a); / / an error will be reported here because an is not initialized
And the following will not report an error:
The copy code is as follows:
String a=string.Empty
Console.WriteLine (a)
Or used to compare:
The copy code is as follows:
If (averse = "")
If (a==string.Empty)
The above two sentences have the same effect.
String.Empty does not allocate storage space
"" allocate a storage space with an empty length
So we usually use string.Empty.
In order to cross-platform in the future, use string.empty.
In C #, "" and string.Empty can be used interchangeably in most cases. For example:
The copy code is as follows:
String s = ""
String S2 = string.Empty
If (s = = string.Empty) {
/ /
}
If statement established
Several writing methods for determining an empty string, in order of performance from high to low, are:
S.Length = = 0 is better than s = = string.Empty is better than s = ""
At this point, I believe you have a deeper understanding of "what are the differences between DBNull.Value,null,String.Empty in asp.net?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.