Reputation: 11146
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)
Upvotes: 11
Views: 2699
Reputation: 273824
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.
Upvotes: 1
Reputation: 700840
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.
Upvotes: 2
Reputation: 499382
You are doing different things here:
ToString()
method of the object. The object returns a string as it was programmed to.null
), no exception will be thrown.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.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.
Upvotes: 17
Reputation: 21204
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 (e.g. "null"
).
Upvotes: 1
Reputation: 36327
There is a difference:
Note that the as operator only performs reference conversions and boxing conversions.
Upvotes: 2
Reputation: 7361
obj as string
(string)obj
"as" operator is safe. if it cannot convert it returns null, when ()operator raises exception
Upvotes: 2