0

The other day I found this statement in a stackoverflow post relating to Extension Methods:

The main thing there is ability to override different methods based on different generic’s parameters instantiation. This is similar to Haskell’s type classes

Java equivalent to C# extension methods

What's that supposed to mean? Can anybody give a significant example which clarifies this statement?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Olaf
  • 3,786
  • 4
  • 25
  • 38
  • Why don't you ask the original poster that question, or ask for clarification. – GreatAndPowerfulOz Jul 14 '16 at 18:13
  • Which part of the question are you looking for clarification on? Would you like clarification on what C# extensions are, how they're used, or how they are similar to Haskell types? – P. Roe Jul 14 '16 at 18:47
  • I'm familiar with Extension Methods. I don't understand the overriding thing based on "generic’s parameters instantiation". That is my central issue. When a comparision to Haskell's type classes can help to explain the concept I would appreciate it. – Olaf Jul 14 '16 at 19:54
  • I put the question to everyone because there may be other beside the author who can offer a good answer. – Olaf Jul 14 '16 at 19:56

1 Answers1

1

I'd assume that they're referring to the fact that C#'s compiler will choose the method that most narrowly defines the type. So for example if you have a abstract class (ABS) and a inherited class (CLS2) and 2 extension methods

public static object GetStuff(this ABS obj){
    blah blah blah
} 

  public static object GetStuff(this CLS obj){
    blah blah blah
} 

if you call the CLS2.GetStuff() the compiler will choose the second method. Once you know that you can take "Override" an extension method by making it more specific. So if you have a generic class

public class Foo<T>{}

You could make 2 extension methods (using classes from above as types)

public static void DoSomething(this Foo<Abs> abs){}

and

public static void DoSomething(this Foo<CLS2> abs){}

Here the second method is "Overriding" the more "generic" abstract type.

This is ony possible with C# because it actually generates a new class for every Generic type. With a language like Java where it uses "Type Erasure Generics" you can't "overload" generic method since everything is really type Object under the hood.

Alex Krupka
  • 710
  • 9
  • 20
  • C# ***does not*** generate a new class for every generic type; it only does this for each usage of the generic type with a struct instantiation of the generic type-parameter. All instantiations of the generic type-parameter share the same class instance. link: https://msdn.microsoft.com/en-us/library/f4a6ta2h.aspx: "*When a generic type is first constructed with a value type as a parameter, the runtime creates a specialized generic type with the supplied parameter or parameters substituted in the appropriate locations in the MSIL.*" – Pieter Geerkens Jul 15 '16 at 03:33
  • continued.: "*Generics work somewhat differently for reference types. The first time a generic type is constructed with any reference type, the runtime creates a specialized generic type with object references substituted for the parameters in the MSIL. Then, every time that a constructed type is instantiated with a reference type as its parameter, regardless of what type it is, the runtime reuses the previously created specialized version of the generic type. This is possible because all references are the same size.*" – Pieter Geerkens Jul 15 '16 at 03:36
  • Semantics (Though I will admit to not having known this before). My point is that you're dealing with the actual type as opposed to type erasure – Alex Krupka Jul 15 '16 at 03:44