Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. So how well do you think you know C#?

So how well do you think you know C#?

Scheduled Pinned Locked Moved The Lounge
csharpphpvisual-studiocomquestion
37 Posts 20 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jorgen Sigvardsson

    Smart? Nah, I give experience credit for this. Also, if you think about it, it has to be this way. Numbers aren't that different from objects in OOP. I think this code makes it more apparent: (boolval ? new Car() : new AirPlane()).Fly();Since C# is statically typed, method selection can't be left to runtime binding. :)

    A Offline
    A Offline
    Artiom Chilaru
    wrote on last edited by
    #25

    In this case to be able to compile it you'll have to manually cast both objects to a base class, that has a Fly() methods :) While in the example of "? 0 : 0.0" the compiler does implicit casting

    1 Reply Last reply
    0
    • L leppie

      Without compiling the following code, what will be the result, if run?

      class Program
      {
      static void Main(string[] args)
      {
      object r = Foo(true);
      Console.WriteLine(r.GetType());
      Console.ReadLine();
      }

      static object Foo(bool e)
      {
      return e ? 0 : 0.0;
      }
      }

      Now compile and run the code? Are you surprised? If so, try figure it out, if not, then kudos to you :)

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      C Offline
      C Offline
      cepi69
      wrote on last edited by
      #26

      I would suggest you to read the "CLR via C#" book, by Jeffery Richter. ;P

      1 Reply Last reply
      0
      • L leppie

        Without compiling the following code, what will be the result, if run?

        class Program
        {
        static void Main(string[] args)
        {
        object r = Foo(true);
        Console.WriteLine(r.GetType());
        Console.ReadLine();
        }

        static object Foo(bool e)
        {
        return e ? 0 : 0.0;
        }
        }

        Now compile and run the code? Are you surprised? If so, try figure it out, if not, then kudos to you :)

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        A Offline
        A Offline
        Alan Balkany
        wrote on last edited by
        #27

        D'ohh! My answer was: "System.Int32". (At least I'm being honest!) Although I haven't tested it, in retrospect it makes sense that the two conditional values should be the same type. Good quiz leppie!

        1 Reply Last reply
        0
        • P PIEBALDconsult

          Yeah, makes sense to me, the ternary operator isn't allowed to return different types, so the int is implicitly cast to double, just as expected. Of course I had to add System. to the Console methods to get it to compile...

          C Offline
          C Offline
          chaiguy1337
          wrote on last edited by
          #28

          Yeah this has caught me a few times, usually with a compile-time error. Kind of annoying.

          “Time and space can be a bitch.” –Gushie, Quantum Leap {o,o}.oO( Looking for a great RSS reader? Try FeedBeast! ) |)””’)            Built with home-grown CodeProject components! -”-”-

          1 Reply Last reply
          0
          • L leppie

            Without compiling the following code, what will be the result, if run?

            class Program
            {
            static void Main(string[] args)
            {
            object r = Foo(true);
            Console.WriteLine(r.GetType());
            Console.ReadLine();
            }

            static object Foo(bool e)
            {
            return e ? 0 : 0.0;
            }
            }

            Now compile and run the code? Are you surprised? If so, try figure it out, if not, then kudos to you :)

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #29

            leppie wrote:

            compile and run the code

            My computer leapt off the table, ran in circles, peed on the carpet, then spent the rest of the day sulking behind the couch, presumably calling Dial-a-Byte on my cellphone over and over again.

            "A Journey of a Thousand Rest Stops Begins with a Single Movement"

            1 Reply Last reply
            0
            • L leppie

              Without compiling the following code, what will be the result, if run?

              class Program
              {
              static void Main(string[] args)
              {
              object r = Foo(true);
              Console.WriteLine(r.GetType());
              Console.ReadLine();
              }

              static object Foo(bool e)
              {
              return e ? 0 : 0.0;
              }
              }

              Now compile and run the code? Are you surprised? If so, try figure it out, if not, then kudos to you :)

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              W Offline
              W Offline
              W i N D
              wrote on last edited by
              #30

              The compiler's said test.cs(6,5): error CS0103: The name 'Console' does not exist in the current context test.cs(7,5): error CS0103: The name 'Console' does not exist in the current context because you aren't import System namespace or use full name like 'System.Console'

              1 Reply Last reply
              0
              • L leppie

                Without compiling the following code, what will be the result, if run?

                class Program
                {
                static void Main(string[] args)
                {
                object r = Foo(true);
                Console.WriteLine(r.GetType());
                Console.ReadLine();
                }

                static object Foo(bool e)
                {
                return e ? 0 : 0.0;
                }
                }

                Now compile and run the code? Are you surprised? If so, try figure it out, if not, then kudos to you :)

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                U Offline
                U Offline
                User 4366736
                wrote on last edited by
                #31

                The Foo method is expecting a reference type (object), but it is getting a value type, probably double. Now the question is, does C# convert the value type to a reference type (boxing)? The reference type would be converted to a Nullable (double?), I don't think so. I don't think it will return a compile error. So I believe it will result in a runtime error (don't know what it would be). I'll run it after this post and see. Tony

                U 1 Reply Last reply
                0
                • U User 4366736

                  The Foo method is expecting a reference type (object), but it is getting a value type, probably double. Now the question is, does C# convert the value type to a reference type (boxing)? The reference type would be converted to a Nullable (double?), I don't think so. I don't think it will return a compile error. So I believe it will result in a runtime error (don't know what it would be). I'll run it after this post and see. Tony

                  U Offline
                  U Offline
                  User 4366736
                  wrote on last edited by
                  #32

                  Interesting, the base type of the Double is a System.ValueType, and it's base type is System.Object. Yes, I am surprised. Thanks for the puzzle.

                  1 Reply Last reply
                  0
                  • J Jorgen Sigvardsson

                    Double I would imagine due to this: return e ? 0 : 0.0;. The thing with this if-expression, is that the true and false types must be of same or assignable types. In this case an int can be cast into a double, but not vice versa. Because of this, the compiler will promote the 0 to a 0.0, hence double.

                    B Offline
                    B Offline
                    Blumen
                    wrote on last edited by
                    #33

                    Hi, I ran this code and irrespective of what the parameter to foo() is, the output is system.Double

                    Jörgen Sigvardsson wrote:

                    The thing with this if-expression, is that the true and false types must be of same or assignable types. In this case an int can be cast into a double, but not vice versa. Because of this, the compiler will promote the 0 to a 0.0, hence double.

                    I din't get this, could you explain this please? Regards, Blumen

                    J 1 Reply Last reply
                    0
                    • B Blumen

                      Hi, I ran this code and irrespective of what the parameter to foo() is, the output is system.Double

                      Jörgen Sigvardsson wrote:

                      The thing with this if-expression, is that the true and false types must be of same or assignable types. In this case an int can be cast into a double, but not vice versa. Because of this, the compiler will promote the 0 to a 0.0, hence double.

                      I din't get this, could you explain this please? Regards, Blumen

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #34

                      If we remodel the if-expression as a function, it could be written like this: T if-expr(bool, T trueExpression, T falseExpression). T is an implicit type generic parameter of sorts. The compiler will at compile time determine what it is. T will not be determined based on what is receiving it. When the compiler sees int var = boolExpr ? trueExpr : falseExpr, it will not try to deduce that T is an int. The compiler will however examine the types of trueExpression and falseExpression. In the original post, we had int and double. Because we said earlier that trueExpression and falseExpression must be of same type T, the compiler must choose between the two types without losing information. An int can "fit inside" a double, but a double can't fit inside an int. Thus the compiler selects T as being double. Since T was chosen to be double, the return type of the entire expression becomes T.

                      -- Kein Mitleid Für Die Mehrheit

                      B 1 Reply Last reply
                      0
                      • J Jorgen Sigvardsson

                        If we remodel the if-expression as a function, it could be written like this: T if-expr(bool, T trueExpression, T falseExpression). T is an implicit type generic parameter of sorts. The compiler will at compile time determine what it is. T will not be determined based on what is receiving it. When the compiler sees int var = boolExpr ? trueExpr : falseExpr, it will not try to deduce that T is an int. The compiler will however examine the types of trueExpression and falseExpression. In the original post, we had int and double. Because we said earlier that trueExpression and falseExpression must be of same type T, the compiler must choose between the two types without losing information. An int can "fit inside" a double, but a double can't fit inside an int. Thus the compiler selects T as being double. Since T was chosen to be double, the return type of the entire expression becomes T.

                        -- Kein Mitleid Für Die Mehrheit

                        B Offline
                        B Offline
                        Blumen
                        wrote on last edited by
                        #35

                        Jörgen Sigvardsson wrote:

                        If we remodel the if-expression as a function, it could be written like this: T if-expr(bool, T trueExpression, T falseExpression). T is an implicit type generic parameter of sorts. The compiler will at compile time determine what it is.

                        I din't understand this because I'm not good with Generics yet. But I understood that if types to be compared are different, compiler makes a choice such that there is no loss of data, I hope I'm correct here. Vielen Dank :)

                        J 1 Reply Last reply
                        0
                        • B Blumen

                          Jörgen Sigvardsson wrote:

                          If we remodel the if-expression as a function, it could be written like this: T if-expr(bool, T trueExpression, T falseExpression). T is an implicit type generic parameter of sorts. The compiler will at compile time determine what it is.

                          I din't understand this because I'm not good with Generics yet. But I understood that if types to be compared are different, compiler makes a choice such that there is no loss of data, I hope I'm correct here. Vielen Dank :)

                          J Offline
                          J Offline
                          Jorgen Sigvardsson
                          wrote on last edited by
                          #36

                          Exactly. A common denominator. :)

                          Blumen wrote:

                          Vielen Dank

                          Bitteschön :)

                          -- Kein Mitleid Für Die Mehrheit

                          1 Reply Last reply
                          0
                          • L leppie

                            Without compiling the following code, what will be the result, if run?

                            class Program
                            {
                            static void Main(string[] args)
                            {
                            object r = Foo(true);
                            Console.WriteLine(r.GetType());
                            Console.ReadLine();
                            }

                            static object Foo(bool e)
                            {
                            return e ? 0 : 0.0;
                            }
                            }

                            Now compile and run the code? Are you surprised? If so, try figure it out, if not, then kudos to you :)

                            xacc.ide - now with TabsToSpaces support
                            IronScheme - 1.0 alpha 4a out now (29 May 2008)

                            E Offline
                            E Offline
                            Elias Haileselassie
                            wrote on last edited by
                            #37

                            class Program
                            {
                            static void Main(string[] args)
                            {
                            object r = Foo(true); //also check for Foo(false);
                            Console.WriteLine(r.GetType());
                            Console.ReadLine();
                            }
                            static object Foo(bool e)
                            {
                            return e ? (object)0 : (object)0.0;
                            }
                            }

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups