[Mono-list] The following code should compile, shouldn't it?
jpo
jpo234@netscape.net
Sat, 21 Dec 2002 18:35:49 +0100
Hi List,
in C/C++ I love to use code like this:
#include <stdio.h>
void foo (int i)
{
printf ("foo got %d\n", i);
}
void bar (int i)
{
printf ("bar got %d\n", i);
}
int
main ()
{
((1 == 1) ? foo : bar)(2);
return 0;
}
The equivalent code in C# should use delegates:
using System;
delegate void foobar (int i);
class Tester {
static void foo (int i)
{
Console.Out.WriteLine ("foo got " + i);
}
static void bar (int i)
{
Console.Out.WriteLine ("bar got " + i);
}
public static void Main ()
{
foobar foo = new foobar (Tester.foo);
foobar bar = new foobar (Tester.bar);
((1 == 1) ? foo : bar) (2);
//foobar baz = ((1 == 1) ? foo : bar);
//baz (2);
}
}
Unfortunately, mcs dislikes it:
mcs ternaryif.cs
syntax error, expecting ASSIGN OP_MULT_ASSIGN OP_DIV_ASSIGN
OP_MOD_ASSIGN OP_ADD_ASSIGN OP_SUB_ASSIGN OP_SHIFT_LEFT_ASSIGN
OP_SHIFT_RIGHT_ASSIGN OP_AND_ASSIGN OP_XOR_ASSIGN OP_OR_ASSIGN
ternaryif.cs(20) error CS1002: Expecting `;'
Compilation failed: 1 error(s), 0 warnings
Is mcs right to complain?
Thanks in advance
Joerg