Type Conversion Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Hetherington

    Type Conversion Question

    Hi,
    Which is the more recomended way to do type conversion in C# for basic data
    types(integer, singles, doubles etc...)
    System.Convert. ToSingle,ToInt3 2 etc...?
    or (float)myvar; (int)myvar, etc..?

    TIA
    --
    Paul


  • Vijaye Raji

    #2
    Re: Type Conversion Question

    In the first case, the runtime does the conversion for you. In the second
    case, the compiler does the conversion. Where possible, if left to me, I'll
    let the compiler do these kind of basic type conversions - because it's done
    once during compile phase and makes my code snappier.

    -vJ

    "Paul Hetherington" <pNhOetShPeArMi ngton@vNiOsSuPa AlMstatement.co m> wrote in
    message news:evY2vB0wDH A.2720@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi,
    > Which is the more recomended way to do type conversion in C# for basic[/color]
    data[color=blue]
    > types(integer, singles, doubles etc...)
    > System.Convert. ToSingle,ToInt3 2 etc...?
    > or (float)myvar; (int)myvar, etc..?
    >
    > TIA
    > --
    > Paul
    >
    >[/color]


    Comment

    • Eric Gunnerson [MS]

      #3
      Re: Type Conversion Question

      To convert between primitive types, generally you would use casts like
      (float) or (int).

      --
      Eric Gunnerson

      Visit the C# product team at http://www.csharp.net
      Eric's blog is at http://blogs.gotdotnet.com/ericgu/

      This posting is provided "AS IS" with no warranties, and confers no rights.
      "Paul Hetherington" <pNhOetShPeArMi ngton@vNiOsSuPa AlMstatement.co m> wrote in
      message news:evY2vB0wDH A.2720@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi,
      > Which is the more recomended way to do type conversion in C# for basic[/color]
      data[color=blue]
      > types(integer, singles, doubles etc...)
      > System.Convert. ToSingle,ToInt3 2 etc...?
      > or (float)myvar; (int)myvar, etc..?
      >
      > TIA
      > --
      > Paul
      >
      >[/color]


      Comment

      Working...