So how well do you think you know C#?
-
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)it will throw null exception.
-
Gary Wheeler wrote:
double, and I'd be surprised pissed off if it were anything else...
I stand corrected :)
Gary Wheeler wrote:
(I'm in the process of learning C#)
From what I remember, you're a C++er. Don't worry, you'll pick it up in no time flat, but you'll hate some stuff. const comes as a[n unpleasant] surprise and so do reflection issues. All in all, its a nice language :)
Don't forget to vote if the response was helpful
Sig history "dad" Ishmail-Samuel Mustafa "You're an idiot." John Simmons, THE Outlaw programmer "I realised that all of my best anecdotes started with "So there we were, pissed". Pete O'Hanlon Unix is a Four Letter Word, and Vi is a Two Letter Abbreviation
Mustafa Ismail Mustafa wrote:
From what I remember, you're a C++er.
Yes indeed. My portion of our next product (the UI) will be written in C#, since we have a requirement to use some WPF controls supplied by another group. Now, I've got a couple thousand pages of books to read to get started :).
Software Zen:
delete this;
-
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)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 theConsole
methods to get it to compile... -
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 theConsole
methods to get it to compile...Personally, I'm always
using
my System. ;P
Visit BoneSoft.com for code generation tools (XML & XSD -> C#, VB, etc...) and some free developer tools as well.
-
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. :)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
-
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) -
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)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!
-
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 theConsole
methods to get it to compile...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! -”-”-
-
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)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"
-
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)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'
-
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)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
-
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
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.
-
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.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
-
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
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 seesint var = boolExpr ? trueExpr : falseExpr
, it will not try to deduce thatT
is an int. The compiler will however examine the types oftrueExpression
andfalseExpression
. In the original post, we hadint
anddouble
. Because we said earlier thattrueExpression
andfalseExpression
must be of same typeT
, the compiler must choose between the two types without losing information. Anint
can "fit inside" adouble
, but adouble
can't fit inside anint
. Thus the compiler selectsT
as beingdouble
. SinceT
was chosen to bedouble
, the return type of the entire expression becomesT
.-- Kein Mitleid Für Die Mehrheit
-
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 seesint var = boolExpr ? trueExpr : falseExpr
, it will not try to deduce thatT
is an int. The compiler will however examine the types oftrueExpression
andfalseExpression
. In the original post, we hadint
anddouble
. Because we said earlier thattrueExpression
andfalseExpression
must be of same typeT
, the compiler must choose between the two types without losing information. Anint
can "fit inside" adouble
, but adouble
can't fit inside anint
. Thus the compiler selectsT
as beingdouble
. SinceT
was chosen to bedouble
, the return type of the entire expression becomesT
.-- Kein Mitleid Für Die Mehrheit
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ö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 :)
Exactly. A common denominator. :)
Blumen wrote:
Vielen Dank
Bitteschön :)
-- Kein Mitleid Für Die Mehrheit
-
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)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;
}
}