Implicit conversion C vs C#

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

    #1

    Implicit conversion C vs C#

    I've recently faced the following problem.
    Say, we have a function in C#.
    void SomeFunction(fl oat f)
    {...}

    Now, we want to call this function and write something like
    SomeFunction(2/3);

    I used the debugger and found out that this call is equivalent to
    SomeFunction(0) ;

    In another words, the system would take the two integer numbers, divide them
    as integers, receive 0, convert to float and pass to the function.

    If I remember well, C would work different. It would convert all the
    arguments of the expression to the most informative type (float in this
    case), calculate the result and pass it to the function.
    Writing SomeFunction(2/3) in C is equivalent to
    SomeFunction((f loat)2/(float)3) in C#.

    Is it "by design", or a bug.


  • Mattias Sjögren

    #2
    Re: Implicit conversion C vs C#

    [color=blue]
    >Is it "by design", or a bug.[/color]

    It's by design. And I think you're wrong about the C behavior.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Max

      #3
      Re: Implicit conversion C vs C#

      I checked it.
      I was wrong.
      It works the same way in C++.
      Sorry.

      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:OaXdK9ilDH A.1764@tk2msftn gp13.phx.gbl...[color=blue]
      >[color=green]
      > >Is it "by design", or a bug.[/color]
      >
      > It's by design. And I think you're wrong about the C behavior.
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/
      > Please reply only to the newsgroup.[/color]


      Comment

      Working...