Tostring () and convert. tostring () usage differences

Source: Internet
Author: User

I. general usage instructions
In C #, all classes inherited from objects have the tostring () method. Without special declarations, classes in C # are inherited from objects by default, therefore, there are tostring () methods, while convert. tostring (PARAM) (where the data type of The param parameter can be basic data types, bool or object class objects;
 

Ii. Differences between tostring () and convert. tostring ()
Generally, both methods can be generic, but if the tostring method is called when a null value may occur in the returned data type, nullreferenceexception is returned, unless you want to capture this exception and then handle it, you should consider using convert in this case. tostring () method, because convert. tostring (null) does not throw an exception but returns an empty string.

3. Application Example
here is a common example of passing webpage parameters, for example, you can obtain the corresponding parameter values from querystring or the current session in the webpage logon verification. The following two methods are available:
1. use convert. tostring (PARAM) method:
string strusername = convert. tostring (session ["username"]);
string struserpwd = convert. tostring (session ["userpwd"]);
2. directly call the tostring () method:
string strusername = session ["username"]. tostring;
string struserpwd = session ["userpwd"]. tostring;
when session ["username"] or session ["userpwd"] returns a null value, the value returned by method 1 is a null string and the tostring () is called directly () method will throw nullreferenceexception. You can decide which method is more suitable based on your actual needs.

Iv. Additional instructions
Generally, there are four methods (such as object OBJ) from object to string, including explicit conversion and use of as keywords: obj. tostring (), convert. tostring (), (string) OBJ, OBJ as string. They can all convert object objects to string objects. Here, I will explain the similarities and differences between them and how to use them in actual programming.
The first two methods are generally string objects obtained from other objects. The differences between them are as follows: when the object to be converted is null, if obj is null, obj is called. the tostring method causes nullreferenceexception and calls convert. tostring does not throw an exception and returns an empty string.

Forced conversion (string) OBJ requires that the runtime type of OBJ must be string. If not, an exception is thrown.
The AS method is relatively stable. If the runtime type of obj is not string, null is returned without throwing an exception.

Therefore, when we need to get the string expression of an object, we should use tostring and convert. tostring. At this time, you have to select one based on the situation. If you can ensure that your object is not null, then the two are similar. If it is possible to be null, you should use convert. tostring. If you want it to be null and throw an exception, you can select tostring.

At last, I would like to add:
Here, object to string. If the first tostring () type is called on other types (such as other classes inherited from the object), the actual content is not necessarily obtained, it may be the description of the class, which mainly depends on how the class implements the tostring () method.

This article originated from 100,000 reasons, author Wu reifeng. Please repost the source and include this link.

I. general usage instructions
In C #, all classes inherited from objects have the tostring () method. Without special declarations, classes in C # are inherited from objects by default, therefore, there are tostring () methods, while convert. tostring (PARAM) (where the data type of The param parameter can be basic data types, bool or object class objects;
 

Ii. Differences between tostring () and convert. tostring ()
Generally, both methods can be generic, but if the tostring method is called when a null value may occur in the returned data type, nullreferenceexception is returned, unless you want to capture this exception and then handle it, you should consider using convert in this case. tostring () method, because convert. tostring (null) does not throw an exception but returns an empty string.

3. Application Example
here is a common example of passing webpage parameters, for example, you can obtain the corresponding parameter values from querystring or the current session in the webpage logon verification. The following two methods are available:
1. use convert. tostring (PARAM) method:
string strusername = convert. tostring (session ["username"]);
string struserpwd = convert. tostring (session ["userpwd"]);
2. directly call the tostring () method:
string strusername = session ["username"]. tostring;
string struserpwd = session ["userpwd"]. tostring;
when session ["username"] or session ["userpwd"] returns a null value, the value returned by method 1 is a null string and the tostring () is called directly () method will throw nullreferenceexception. You can decide which method is more suitable based on your actual needs.

Iv. Additional instructions
Generally, there are four methods (such as object OBJ) from object to string, including explicit conversion and use of as keywords: obj. tostring (), convert. tostring (), (string) OBJ, OBJ as string. They can all convert object objects to string objects. Here, I will explain the similarities and differences between them and how to use them in actual programming.
The first two methods are generally string objects obtained from other objects. The differences between them are as follows: when the object to be converted is null, if obj is null, obj is called. the tostring method causes nullreferenceexception and calls convert. tostring does not throw an exception and returns an empty string.

Forced conversion (string) OBJ requires that the runtime type of OBJ must be string. If not, an exception is thrown.
The AS method is relatively stable. If the runtime type of obj is not string, null is returned without throwing an exception.

Therefore, when we need to get the string expression of an object, we should use tostring and convert. tostring. At this time, you have to select one based on the situation. If you can ensure that your object is not null, then the two are similar. If it is possible to be null, you should use convert. tostring. If you want it to be null and throw an exception, you can select tostring.

At last, I would like to add:
Here, object to string. If the first tostring () type is called on other types (such as other classes inherited from the object), the actual content is not necessarily obtained, it may be the description of the class, which mainly depends on how the class implements the tostring () method.

This article originated from 100,000 reasons, author Wu reifeng. Please repost the source and include this link.

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.