4

I have the following code:

static Func<object, string> s_objToString = (x) => x.ToString();
static Func<string, string> s_stringToString = s_objToString; //compiles
static Func<int, string> s_intToString = s_objToString; //error

The second line compiles but the third line fails to compile with error:

Cannot implicitly convert type 'System.Func<object,string>' to 'System.Func<int,string>'

Why is that?

I understand that with genetics although string is derived from object a List<string> does not derive from List<object>, but here object to string works and object to int fails, why?

OK let's say I understood why; the question now is there a way around it (other then defining MyInt class to box int because Func<object,string> to Func<MyInt,string> works)?

nawfal
  • 70,104
  • 56
  • 326
  • 368
Roey Nissim
  • 555
  • 8
  • 25
  • 3
    It's always worth checking the preview for a question, especially when you're asking questions about generics (or anything else that uses `<>` angle brackets), to make sure that your text is showing up or mark it up accordingly. – Damien_The_Unbeliever Oct 23 '12 at 07:08
  • @Roey You can box an `int` just by casting it to `object`, (or passing it to `s_objToString`!), you don't need a new class to do that. – Rawling Oct 23 '12 at 08:03
  • passing it to `s_objToString` or casting it to object might work on a simple solution. but my issue is with functions like: `public static string codeList(List thelist, Func coder)` for details see [new question](http://stackoverflow.com/questions/13026128/implemting-genetics-in-a-function-using-func) – Roey Nissim Oct 23 '12 at 08:20

2 Answers2

5

It is because Func is defined as Func<in T, out TResult>, MSDN is here, so T is contra-variant with in keyword, that is, you can use either the type you specified or any type that is less derived, but remember that co-variance and contra-variance do not support for value type:

Why covariance and contravariance do not support value type

So, it works for string but does not work out with int. You might need to read more about covariance and contravariance:

http://msdn.microsoft.com/en-us/library/dd233060.aspx

http://msdn.microsoft.com/en-us/library/dd799517.aspx

Community
  • 1
  • 1
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • Ok, thanks. Is there a way around it? other then defining MyInt class to box int cause Func to Func work – Roey Nissim Oct 23 '12 at 07:53
  • @RoeyNissim: What I would say, you should consider your design to avoid this case. Btw, you should post another question instead of update question in here to ask another problem – cuongle Oct 23 '12 at 08:01
3

Because co/contra-variance doesn't work for value types.

Please take a look here

Variance is supported only if a type parameter is a reference type. Variance is not supported for value types.
The following doesn’t compile either:

// int is a value type, so the code doesn't compile.
IEnumerable<Object> objects = new List<int>(); // Compiler error here.
petro.sidlovskyy
  • 5,075
  • 1
  • 25
  • 29
  • irreverent, the following does not compile with string either static List objs = new List(); – Roey Nissim Oct 23 '12 at 07:17
  • @RoeyNissim: concept covariance and contra-variance just works with generic interface and delegate, so List is invariant, not covariant – cuongle Oct 23 '12 at 07:21
  • @RoeyNissim @CuongLe: This answer is correct, though bad example `List` here. The compile error in the answer results from invalid cast from `IEnumerable` to `IEnumerable` per-se. – Cheng Chen Oct 23 '12 at 07:28
  • @DannyChen: yes, it's correct, I just tried to explain for @RoeyNissim why the `List objs = new List()` does not work – cuongle Oct 23 '12 at 07:31