Re: casting vs. using the "Convert" methods?

From: Alexander Mueller (postmaster_at_127.0.0.1)
Date: 03/18/05


Date: Fri, 18 Mar 2005 03:15:01 +0100

Bruce Wood wrote:
> Not true. The truth is a bit more complex than that. .NET provides
> three methods of getting from point A to point B, as it were.

When I was talking of casting I was of course only talking about casting
in terms of objects and not of primitive types.

>
> Note that the underlying representation _did_ change in this
> conversion: a double is represented completely differently from an int.

Well, because it was a conversion and not a cast.

>
> Note that there is nothing that absolutely requires a method call here.
> The designers could quite easily have created an explicit
> cast operator that converted a string to an int. The requirement that
> you call a method is a stylistic choice rather than a fundamental
> requirement of the language.

As proven by PHP.

> Implicit and explicit casts are method calls too (that's how you make
> your own).

I have to say I am not extremely well familiar with C# internals but I
doubt it calls a method for a simple cast. Casting an instance from
object to string will probably only tell the compiler not to report the
typical incompatible types message - along with additional checks for
possible unsupported casting exceptions.



Relevant Pages

  • Re: Collections
    ... cast an abstract class into a derived class ... First remember that DirectCast is the Cast operator, while CType is the ... Conversion operator that will do a Cast if the cast is allowed. ... remember that Casting & Converting are separate ...
    (microsoft.public.dotnet.languages.vb)
  • Re: about casting
    ... while I see X as string, the same as DirectCast ... cast for: cast but exclude any indirect conversion. ... what we've been talking about as a "direct cast" just to distinguish ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: what is this c# syntax?
    ... You mention casting an array to a string. ... You can't cast an array to a string because one is not an ancestor of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Casting to double in C#
    ... catch/Handle exception here. ... Or you can convert the value to string and then parse - better avoid ... Cast, not convert, the object to string. ... conversion, and an explicit cast should be far more efficient than calling ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Huge string arrays in C#
    ... > in addition, string is a reference type, not a value type, so the casting ... There is no cast to assign a string to an object variable, ...
    (microsoft.public.dotnet.framework.performance)

Loading