*int myint = b ? 1 : 2; Because exists the conversion because int is a well-known datatype in .net.* Sorry... *int myint = b ? 1 : 2; It works because int is a well-known datatype in .net and you don't have to make any conversion.*
On Thu, May 21, 2009 at 9:57 PM, Paulo Alexandre Costa < [email protected]> wrote: > Hi Joe Enos > > I'm not so sure about the problem but.... > > when you use the operator ":", the left side (when the clausule is true) > and right (when it's false) has to have the same type > > int myint = b ? 1 : 2; > Because exists the conversion because int is a well-known datatype in .net. > > > When you pass the name of two methods > > Func<int> myFunc = b ? DoSomething : DoSomethingElse; > > The compiler screams: > * there is no implicit conversion between 'method group' and 'method > group'* > > I think the compiler don't know exactly the type, so you have to inform to > the compiler explicitily > > So see this > > class Teste > { > public static int DoSomething() { return 1; } > public static int DoSomethingElse() { return 2; } > public static bool GetSomeBoolean(){return true;} > > public static void Main(string[] args) > { > bool b = GetSomeBoolean(); > Func<int> myFunc = b ? (Func<int>)DoSomething : > (Func<int>)DoSomethingElse; > System.Console.WriteLine(myFunc().ToString()); > } > } > > public delegate int Func<T>(); > > Compile ? yes > execute ? yes > > So, I think the reason is that you have to inform to the compiler explicity > the type. Since delegate is a class, you can use a simple explicit cast > (Func<int>), ok ? > > I am not so sure....but i think this is the way... > > Best Regards > > > > Does > > > On Thu, May 21, 2009 at 4:59 PM, Joe Enos <[email protected]> wrote: > >> >> Kind of a weird issue here - not really a problem, but I'm hoping >> someone can explain it... >> >> Suppose I have a delegate variable and want to assign to it >> conditionally based on the value of a boolean. For example: >> >> public static int DoSomething() { return 1; } >> public static int DoSomethingElse() { return 2; } >> >> public static void Main(string[] args) >> { >> bool b = GetSomeBoolean(); >> >> /* Method #1 - This does not work - gives me a compile-time error: >> error CS0173: Type of conditional expression cannot be determined >> because there is no implicit conversion between 'method group' and >> 'method group' >> */ >> Func<int> myFunc = b ? DoSomething : DoSomethingElse; >> >> // the following three work fine >> >> /* Method #2 */ >> Func<int> myFunc; >> if (b) myFunc = DoSomething; else myFunc = DoSomethingElse; >> >> /* Method #3 */ >> Func<int> myFunc = b ? new Func<int>(DoSomething) : >> DoSomethingElse; >> >> /* Method #4 */ >> Func<int> myFunc = b ? DoSomething : new Func<int> >> (DoSomethingElse); >> } >> >> Any ideas on why method #1 doesn't work? Obviously there are at least >> three other ways of writing this statement (#2,3,4), but I'm curious >> as to why #1 fails at compile time, and even more curious why 2, 3, >> and 4 work when 1 doesn't. #3 and #4 are identical to #1, except one >> method group is wrapped inside of a constructor for Func, which I >> thought was always optional. >> >> No big deal, but it's just bugging me... >> >> Thanks >> >> Joe > > > > > -- > Paulo Alexandre Costa > Universidade Estadual do Ceará - UECE > Great-Ufc Desenvolvedor .Net > Linux User #464032 > "A velocidade impressionante dos computadores é de utilidade limitada se os > programas que neles rodam usam algoritmos ineficientes." > > > -- Paulo Alexandre Costa Universidade Estadual do Ceará - UECE Great-Ufc Desenvolvedor .Net Linux User #464032 "A velocidade impressionante dos computadores é de utilidade limitada se os programas que neles rodam usam algoritmos ineficientes."
