简体   繁体   中英

Difference between casting/conversion methods in C#

there are many ways to cast/convert object to another by what the difference between those and if there is no difference why there are so many ways to achieve one thing? Isn't that damage to language?

let's say object obj to string.

obj.ToString()
obj as string
(string)obj
Convert.ToString(obj)

You are doing different things here:

  • obj.ToString() - this is a call to the ToString() method of the object. The object returns a string as it was programmed to.
  • obj as string - this is an attempt to convert the object to a string, which may or may not fail (if it fails, the result is null ), no exception will be thrown.
  • (string)obj - this is an explicit cast of obj to the type string , you are telling the compiler that obj is a string . If obj is not a string type, you will get a cast exception.
  • Convert.ToString(obj) - This is an explicit call to the Convert class to return a string representation of obj .

There are these many different ways to get a string from an object because each one is different and have subtle differences. Yes, in many cases the returned string will be the same, but this is not guaranteed.

obj as string
(string)obj

"as" operator is safe. if it cannot convert it returns null, when ()operator raises exception

There is a difference:

  • ToString is a method which relies on that the object itself defines what the string representation will look like.
  • The as keyword is reference conversion: Note that the as operator only performs reference conversions and boxing conversions.
  • Using () to specify the type is an explicit conversion whereas the as keyword is implicit .
  • Using the Convert -class actually just helps you parse it, the Convert.ToString probably just wraps .ToString() on the object.

There are often several ways of doing basically the same thing, but with small variations. There is a distinct difference between casting and converting, where you for example can convert an int to string , but you can't cast an int to string .

You have a valid point in whether this is damaging for the language, but not mainly because there are many ways of doing things, but because they may be inconsistently implemented. When you implement classes, you have to be careful so that they behave as expected. Checking for equality is one example, where you can either use a method or an operator:

x == y
x.Equals(y)

If you implement one of them in your class but not the other, the compiler helps you with a warning that both has to be implemented for the class to behave properly.

First of all, casting is very different from converting an object to a string. Casting is not converting an object into anything, it just assumes another type, whereas ToString really creates a string from an object (this may of course be a no-op if the object is already a string).

There are two casting operations for the reason Arseny explained: The as operator will return a null-reference whereas the () operator will raise an exception if the cast is not possible.

There are two ToString methods, as obj.ToString() of course only works if obj is really an object and not for instance an int. For the later case you have to use Convert.ToString() . Similarly, when obj is null , then obj.ToString() will raise an exception, whereas Convert.ToString() can return some sensible string (eg "null" ).

what [is] the difference between those

There are 2 ways of type-casting and a range of classes and methods in the library. Typecasting is used for objects (values) that are already technically close and strongly related.

Conversion (the Convert class and SomeType.Parse() methods) are really converting datatypes (String to Int32 cannot be a cast).

why there are so many ways to achieve one thing?

Casting and Conversion are important, frequently used actions. That is why we have a fine-grained toolset, admittedly with quite a bit of overlap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM