2

I am attempting to build a Dictionary that looks like this:

var getValueFuncs = new Dictionary<TypeID, Func<string, object>>
{
  {TypeID.BINARY, NotMine.GetBinaryValue},
  {TypeID.BOOLEAN, NotMine.GetBooleanValue},
  {TypeID.DATE, NotMine.GetDateTimeValue},
  {TypeID.DOUBLE, NotMine.GetFloat64Value},
  {TypeID.LONG, NotMine.GetInteger32Value},
  {TypeID.STRING, NotMine.GetStringValue}
};

The various Funcs that I want to put into it all have different return types.

public interface INotMine : ICollection, ICloneable
{
  byte[] GetBinaryValue(string propName);
  bool? GetBooleanValue(string propName);
  DateTime? GetDateTimeValue(string propName);
  double? GetFloat64Value(string propName);
  int? GetInteger32Value(string propName);
  string GetStringValue(string propName);
}

Note that NotMine is a separate class, that it inherits from INotMine, and that I cannot change it.

The two lines to initialize the Dictionary with the procedures that return byte[] and string compile with no trouble. The four lines for procedures that return nullable data types (i.e. bool?, DateTime?, double?, and int?) will not compile. They each report an error like this one:

bool? INotMine.GetBooleanValue(string propName) 
'bool? INotMine.GetBooleanValue(string)' has the wrong return type 
Expected a method with 'object GetBooleanValue(string)' signature

Given that everything inherits from object, why will those lines not compile and go into my Dictionary nicely?

EDIT: Before he deleted it, someone posted an answer that contained an elegant work-around that looked like this:

    {TypeID.BINARY, s => NotMine.GetBinaryValue(s)}

I'm grateful for that, and I'll use that, but I'd still like to know why the original lines won't compile.

1
  • 1
    They don't compile because the C# compiler refuses to implicitly emit a wrapper delegate to convert the value type Nullable<T> to an object, which requires a boxing conversion (this is essentially what the workaround provides). Yes, every type nominally inherits from object, including value types, but they're still treated specially. Feb 4, 2020 at 22:17

1 Answer 1

4

Consider the following:

Func<Animal, Turtle> fat = whatever;
Func<Mammal, Reptile> fmr = fat;

This is legal. fat can take any Animal, and the caller of fmr promises to pass a Mammal, which is always an Animal. Similarly fmr requires that the function return a Reptile, and fat always returns a Turtle, which is a Reptile.

This kind of conversion is called a variant conversion. Specifically, the "I require any Animal so I'll accept Mammal" is called a contravariant conversion, and the "I return a Turtle so I meet your need for a Reptile" is a covariant conversion.

C# supports covariant and contravariant conversions on generic types only under these circumstances:

  • The type in question is a delegate or interface.
  • The type has been marked as safe for variance, and the compiler has confirmed that it is safe.
  • The type arguments that are varying are all reference types.

You meet the first two conditions -- your Func<string, object> is a delegate type known to be safe to be contravariant in the argument and covariant in the return. However you do not meet the third condition. bool? DateTime?, double? and int? are not reference types. Therefore, variant conversion rules do not apply, and the conversions are illegal.

That would cover converting from Func<string, bool?> to Func<string, object>. But that's not what you're doing; you are converting from the method group containing a function that returns bool?. Does that matter?

No. The rules are the same for method group conversions. A covariant conversion from a method group containing a method that returns bool? to a Func<string, object> is not legal because bool? is still required to be a reference type to make that conversion legal.

No matter how you slice it, you are stuck. You cannot convert a method or delegate that returns bool? directly to a delegate that returns object.

The reason for this is summed up nicely in Jeroen's comment on the question. Where does the boxing conversion go? A boxing conversion is code that allocates heap memory and therefore that code has to go somewhere, but there is nowhere for the compiler to put it and still maintain reference identity on the delegate.

As you correctly note, if you provide your own lambda then the compiler has a place to put the boxing conversion; it sticks it on top of the return from the lambda. The price you pay for this is that the lambda is now a delegate that refers to the lambda body, and not directly to GetBooleanValue, so there is a tiny indirection penalty there.

4
  • Why is it an issue for the compiler and/or runtime to "maintain reference identity on the delegate"? Would something really important break if a wrapper was generated under water and used instead? In most scenarios (including this particular one) it would not be an issue since the programmer typically doesn't care about the identity of delegates (and scenarios where you do, like event handlers, are distinguishable). Is this a "we can't make this work transparently in all cases so just don't and make them write it out" scenario? Feb 5, 2020 at 10:30
  • @JeroenMostert: What we have here is a good example of how C# and VB differ. Both take principled approaches but the principles are different! VB is a "muddle through and do what the developer probably meant so that the program works" language; VB will build a wrapper. C# is a "do what the developer says and say if something looks wrong" language; C# assumes that if you are doing what looks like a reference conversion then you wish to maintain reference identity, and will refuse to do the conversion if it can't be done. Feb 5, 2020 at 15:43
  • Well, we could content both C# and the developer by inventing a brand new "super-duper covariant delegate conversion" that includes building a wrapper when necessary, and then what the developer is doing and what C# thinks they're doing would match up. :-) My question was not so much why C# refuses to compromise on its conversion rules as a matter of practicality, but what roadblock there is or was against the conversion rules being more sophisticated to cover this in a non-muddled fashion. (That might be a "minus 100 points" scenario, I don't know.) Feb 5, 2020 at 15:57
  • @JeroenMostert: C# could design and implement that rule, sure; VB did. Cases like these come down to judgment calls where you have to balance a great many competing design principles evaluated in an absence of data. Plainly this decision was confusing to at least one user because they posted a question here, but does removing that source of confusion for a handful of developers pay for the potential confusion of developers who think that they can compare reference-converted delegates for reference equality and not be surprised by the results? Hard to say. Feb 5, 2020 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.