Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about generics in C# comparing to Java

In Java I can specify generic with wildcard "?". It is possible to create a map like this one:

Map<String, ?>.

I'm working with C# and I need a Dictionary<String, SomeInterface<?>> (where ? can be int, double, any type). Is this possible in C#?

EDIT:

Example:

interface ISomeInterface<out T>{
 T Method();
 void methodII();
}

class ObjectI : ISomeInterface<int>{
    ...
}
class ObjectII : ISomeInterface<double>{
    ...
}
class ObjectIII : ISomeInterface<string>{
    ....
}

I was trying to map this objects into Dictionary like:

Dictionary<String, ISomeInterface<?>> _objs = new Dictionary<String, ISomeInterface<?>();

_objs.Add("Object1", new ObjectI());
_objs.Add("Object2", new ObjectII());
_objs.Add("Object3", new ObjectII());

foreach(var keyVal in _objs){
   Console.WriteLine(keyVal.Method());
}

Objects that implement ISomeInterface are loaded in runtime using Assembly and Activator.createInstance. In the moment of creation I don't if objects implements ISomeInterface<int> or ISomeInterface<double>.

Any help is very much appreciated.

like image 965
aumanets Avatar asked May 12 '11 15:05

aumanets


People also ask

What problems can be solved using generics?

In a nutshell, generics solves the problem of having to use loosely typed objects. For example, consider ArrayList vs List<T> . It allows you to have a strongly typed collection. list[0] will return type T vs arrayList[0] which will return type object .

What are the constraints in generics?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.

What are generics in C# Interview Questions?

Generics refers to the technique of writing the code for a class without specifying the data type(s) that the class works on. It allow you to define type-safe data structures, without committing to actual data types.


Video Answer


3 Answers

No.

However, if you're using C# 4, you can make ISomeInterface covariant so that ISomeInterface<Anything> will be convertible to ISomeInterface<object>.

If ISomeInterface has methods that take parameters of its type parameter (as opposed to return values), this will be completely impossible, since it would then allow you to pass arbitrary objects as the parameters.

EDIT: In your specific case, the best solution is to make IMyInterface<T> inherit a separate non-generic IMyInterface interface and move all members that don't involve T to the base interface.
You can then use a Dictionary<string, IMyInterface> and you won't have any trouble.

like image 113
SLaks Avatar answered Oct 26 '22 23:10

SLaks


There is the possibility to restrict your type variables to certain types:

public class Map<U, T> where T : IInterface
{
}

However, you can't do something like:

Map<string, T> map = new Map<string, T>()
like image 28
Femaref Avatar answered Oct 27 '22 01:10

Femaref


For the usage you're describing, you could use a workaround, with an IDictionary<string, ISomeInterface>:

interface ISomeInterface
{
    object Method();
    void Method2();
}

interface ISomeInterface<T> : ISomeInterface
{
    T Method();
}

class C1 : ISomeInterface<int>
{
    object ISomeInterface.Method() { return Method(); }
    public int Method() { return 10; }
    public void Method2() { }
}
like image 44
Hosam Aly Avatar answered Oct 26 '22 23:10

Hosam Aly