Discussion on the difference between ToString of and Convert. ToString of in C

  • 2021-11-13 17:56:27
  • OfStack

Discussion on the difference between ToString () and Convert. ToString () methods

1. 1 General Instructions for Usage

ToString () is an extension of Object, so there are ToString () methods; And Convert. ToString (param) (where the data type of the param parameter can be various basic data types, and can also be bool or object class objects.

2. Differences between ToString () and Convert. ToString ()

ToString will return NullReferenceException if the ToString method is called when null value is possible in the returned data type. Unless you want to catch this exception and handle it again, you should consider using Convert. ToString () method in this case, because Convert. ToString (null) will not throw an exception but return an empty string.
The main difference is as shown above, because ToString () is an extended method, extended from Object, so turn to null to report an exception. Convert. ToString () returns an empty string.

However, Convert. ToString () is not very useful because:


static void Main(string[] args)
    {
      string str1 = "";
      Console.WriteLine(Convert.ToString(str1) == null); //false
      Console.WriteLine(Convert.ToString(str1) == "");  //true

      string str2 = null;
      Console.WriteLine(Convert.ToString(str2) == null); //true
      Console.WriteLine(Convert.ToString(str2) == "");  //false

      Console.ReadKey();
    }

null is still null after it is turned, and "it is still" after it is turned.

Therefore, it is more convenient to cooperate with string. IsNullOrEmpty (Convert. ToString ()).


    Console.WriteLine(string.IsNullOrEmpty(Convert.ToString(str1)));  //true
    Console.WriteLine(string.IsNullOrEmpty(Convert.ToString(str1)));  //true

In addition, it is very convenient to use Convert. ToString () if it is compared with a string, for example


      if(Convert.ToString(str) == "123")
      {

      }

3. Conversion from object to string

There are roughly four ways from object to string, including explicit conversion and the use of as keywords: obj. ToString (), Convert. ToString (), (string) obj, obj as string. They can all convert object objects into string objects.

The first two methods usually get string objects from other objects. The differences between them are mainly manifested as mentioned above:

ToString (): If obj is null, calling the obj. ToString () method causes an NullReferenceException exception.

Convert. ToString (): If obj is null, calling Convert. ToString () returns null

(string): To cast (string) obj requires that the runtime type of obj must be string. If not, an exception is thrown.

as: The as method is relatively smooth, returning null without throwing an exception when the runtime type of obj is not string.

So normally, when we need to get an string representation of an object, we should use ToString () and Convert. ToString (), so you have to choose one according to the situation, and if you can guarantee that your object is not null, the two are similar. If it is possible to be null, you should use Convert. ToString (). If you want it to throw an exception when it is null, you can certainly choose ToString ().

The method of ToString () is so convenient that it is thought that as far as it is concerned, it is generally judged whether it is null before turning.

The above is the whole content of this article, hoping to help everyone!


Related articles: