Monday, March 28, 2011

C# Redundant Delegate

public delegate T GetObject<T>(SqlDataReader reader);
public delegate KeyValuePair<TKey, TValue> GetObject<TKey, TValue>(SqlDataReader reader);

Is it possible for removing that 2nd line (the two lines are on consecutive lines) to ever make any difference at all, superficial or not?

From stackoverflow
  • Absolutely - anything which is trying to declare a value of type GetObject<string,string> will fail to compile, for example.

    Now, you can certainly replace any use of GetObject<TKey,TValue> with GetObject<KeyValuePair<TKey,TValue>> (if you see what I mean) but you can't just remove the second line.

    You should also be careful of the case where this is used by reflection. It may well not be a problem in your case, but it's an easy way for a breaking change to only be visible at execution time rather than being picked up by the compiler.

  • Can it make a difference. Most definitely. Removing this delegate definition will cause the formerly valid code to be a compilation error.

    var x = new GetObject<int,String>(SomeFunction);
    

0 comments:

Post a Comment