Why doesn’t this compile? I suppose there are ways to work around this; just curious.
Thanks!
static void Main(string[] args)
{
Func<int, int> f_int = n => n * n;
Func<int, double> f_double = (Func<int, double>)f_int;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generics don’t have this type of variance; not now, and not (for value-types such as
int/double) in 4.0. Simply,f_intdoesn’t return adouble. The best you can do is:which has an implicit conversion from
inttodoublein the returned value.