I’m a newer in c# so maybe my question will seem naive to some of you.
I’m using this method:
public static object ChangeType(object value, Type conversionType);
From metadata manual:
Returns:
An object whose type is conversionType and whose value is equivalent
to value.-or-A
null reference (Nothing in Visual Basic), if value is null and conversionType
is not a value type.
But in method signature the returned type always ‘object’. So, what is the benefit in converting the value if returned type is object?
The reason is that you can cast to your particular type. This method comes from before generic types so the only way it could return one of any number of types is by returning the supertype of all of them, ie object. You can of course then cast that to your chosen type and guarantee you will be successful but the only other option would be an overload of every single type ever which would be a bit weighty. 🙂
The thing to note is that this isn’t just the same as casting. It will return a whole new object which is the type you asked for rather than the type you gave it.