32

Let's say I have a generic class as follows:

public class GeneralPropertyMap<T>
{
}

In some other class I have a method that takes in an array of GeneralPropertyMap<T>. In Java, in order to take in an array that contains any type of GeneralPropertyMap the method would look like this:

private void TakeGeneralPropertyMap(GeneralPropertyMap<?>[] maps)
{
}

We use the wildcard so that later we can call TakeGeneralPropertyMap passing a bunch of GeneralPropertyMap with any type for T each, like this:

GeneralPropertyMap<?>[] maps = new GeneralPropertyMap<?>[3];
maps[0] = new GeneralPropertyMap<String>();
maps[1] = new GeneralPropertyMap<Integer>();
maps[2] = new GeneralPropertyMap<Double>();
//And finally pass the array in.
TakeGeneralPropertyMap(maps);

I'm trying to figure out an equivalent in C# with no success. Any ideas?

AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61
  • Have you tried making the function take `GeneralPropertyMap[] maps` .. I guess it should work because of Covariance – Ankur Mar 22 '13 at 16:22

5 Answers5

24

Generics in C# make stronger guarantees than generics in Java. Therefore, to do what you want in C#, you have to let the GeneralPropertyMap<T> class inherit from a non-generic version of that class (or interface).

public class GeneralPropertyMap<T> : GeneralPropertyMap
{
}

public class GeneralPropertyMap
{
    // Only you can implement it:
    internal GeneralPropertyMap() { }
}

Now you can do:

private void TakeGeneralPropertyMap(GeneralPropertyMap[] maps)
{
}

And:

GeneralPropertyMap[] maps = new GeneralPropertyMap[3];
maps[0] = new GeneralPropertyMap<String>();
maps[1] = new GeneralPropertyMap<Integer>();
maps[2] = new GeneralPropertyMap<Double>();
TakeGeneralPropertyMap(maps);
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • 6
    It's still not good enough since in the interface you will most likely want to have a method that returns or takes a wildcard to use from the "TakeGeneralPropertyMap" method, which you can't do because we don't want the interface to be of a generic type. However, this answer at least gets close. Thanks :-) – AxiomaticNexus Apr 24 '13 at 14:57
12

While, as others have noted, there's no exact correspondence to wildcards in c#, some of their use cases can be covered with covariance/contravariance.

public interface IGeneralPropertyMap<out T> {} // a class can't be covariant, so 
                                        // we need to introduce an interface...

public class GeneralPropertyMap<T> : IGeneralPropertyMap<T> {} // .. and have our class
                                                            // inherit from it

//now our method becomes something like
private void TakeGeneralPropertyMap<T>(IList<IGeneralPropertyMap<T>> maps){}

// and you can do
    var maps = new List<IGeneralPropertyMap<Object>> {
        new GeneralPropertyMap<String>(),
        new GeneralPropertyMap<Regex>()
    };
    //And finally pass the array in.
    TakeGeneralPropertyMap<Object>(maps);

The caveat is that you can't use covariance with value types, so adding a new GeneralPropertyMap<int>() to our list fails at compile time.

cannot convert from 'GeneralPropertyMap<int>' to 'IGeneralPropertyMap<object>'

This approach may be more convenient than having a non-generic version of your classes/interfaces in case you want to constrain the types that GeneralPropertyMap can contain. In that case:

public interface IMyType {}
public class A : IMyType {}
public class B : IMyType {}
public class C : IMyType {}

public interface IGeneralPropertyMap<out T> where T : IMyType {} 

allows you to have:

var maps = new List<IGeneralPropertyMap<IMyType>> {
    new GeneralPropertyMap<A>(),
    new GeneralPropertyMap<B>() ,
    new GeneralPropertyMap<C>() 
};
TakeGeneralPropertyMap(maps);
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
5

There is no direct equivalent to this in C#.

In C#, this would often be done by having your generic class implement a non-generic interface or base class:

interface IPropertyMap
{
   // Shared properties
}

public class GeneralPropertyMap<T> : IPropertyMap
{
}

You could then pass an array of these:

IPropertyMap[] maps = new IPropertyMap[3];
// ...

TakePropertyMap(maps);
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I understand the Generic methods, but they won't do in this case. Every time I call it, I can only specify one type for the GeneralPropertyMap. So I can only call it passing either an array of GeneralPropertyMap[], or GeneralPropertyMap, and so on. – AxiomaticNexus Mar 22 '13 at 16:19
0

Actually, you can get pretty close to a wildcard by using dynamic. This also works nicely if you have a non-generic superclass.

For example:

public class A
{
  // ...
}

public class B<T> : A
{
  // ...
}

public class Program
{
  public static A MakeA() { return new A(); }

  public static A MakeB() { return new B<string>(); }

  public static void Visit<T>(B<T> b)
  {
    Console.WriteLine("This is B with type "+typeof(T).FullName);
  }

  public static void Visit(A a)
  {
    Console.WriteLine("This is A");
  }

  public static void Main()
  {
    A instA = MakeA();
    A instB = MakeB();

    // This calls the appropriate methods.
    Visit((dynamic)instA);
    Visit((dynamic)instB);

    // This calls Visit(A a) twice.
    Visit(instA);
    Visit(instB);
  }
}

How this works is explained in the C# documentation here.

Wouter
  • 538
  • 6
  • 15
-1

Make an interface from the members of GeneralPropertyMap (IGeneralPropertyMap), and then take an IGeneralPropertyMap[] as an argument.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62