Conversion of the type of a Template class?

HashMap is a Java type. Also it generally has two type parameters rather than one In C#, generic classes are always invariant Some interfaces will be variant in C# 4, but very few (only those which either only use the type parameter in an output positiion, e. G IEnumerable() method EDIT: Okay, with HashSet baseSet = ...; var derivedSet = new HashSet(baseSet.Cast()) Note that even with C# 4 this would be necessary because the compiler doesn't know that every value in baseSet is an instance of DerivedType there has to be an execution-time check.

The reverse (creating a HashSet baseSet = ...; HashSet derivedSet = ...; derivedSet. UnionWith(baseSet.Cast()).

HashMap is a Java type. Also it generally has two type parameters rather than one... In C#, generic classes are always invariant. Some interfaces will be variant in C# 4, but very few (only those which either only use the type parameter in an output positiion, e.g. IEnumerable, or only use the type parameter in an input position, e.g. IComparable).

If you can provide more precise information about your situation, we'll probably be able to help come up with a simple solution - particularly if you can use LINQ with its Cast() method. EDIT: Okay, with HashSet: HashSet baseSet = ...; var derivedSet = new HashSet(baseSet.Cast()); Note that even with C# 4 this would be necessary because the compiler doesn't know that every value in baseSet is an instance of DerivedType - there has to be an execution-time check. The reverse (creating a HashSet from a HashSet) would work in C# 4.

FURTHER EDIT: If you just want to use UnionWith, that doesn't require a HashSet - it requires an IEnumerable. I suggest you do: HashSet baseSet = ...; HashSet derivedSet = ...; derivedSet. UnionWith(baseSet.Cast()).

Arr ... I ment HashSet, not HashMap. From your answer I read, that it is not possible to convert the HashSet-Type and do have to do it manually, right? What is behind the question: I want to use the Union-method of the HashSet, so I need to provide a Parameter of the Type HashSet, but my function returns HashSet – Frank Schumacher Jul 3 '09 at 9:19 The UnionWith-method, not Union-method – Frank Schumacher Jul 3 '09 at 9:26 Ah, using UnionWith is simpler - see my second edit.

– Jon Skeet Jul 3 '09 at 9:29 the Cast() is what I was looking for, many thanks! – Frank Schumacher Jul 3 '09 at 9:31.

Here's the solution //**Your BaseClass** public class BaseClass where T : BaseClass { public HashSet GetHashSet() { HashSet _hSet = new HashSet(); //do some work //create a HashSet and return; return _hSet; } } //**Your Inherited/Derived Class** public class InheritedClass : BaseClass { //you have the method inherited as you need. } }.

I would suggest adding a where clause to the generic type: where T : BaseClass – Oliver Hanappi Jul 3 '09 at 9:46 @Oliver: thanks oliver, I added a proper where clause now. It seems ok now? – this.

__curious_geek Jul 3 '09 at 9:57.

HashMap is a Java type. In C#, generic classes are always invariant. Some interfaces will be variant in C# 4, but very few (only those which either only use the type parameter in an output positiion, e.g. IEnumerable, or only use the type parameter in an input position, e.g. If you can provide more precise information about your situation, we'll probably be able to help come up with a simple solution - particularly if you can use LINQ with its Cast() method.

Note that even with C# 4 this would be necessary because the compiler doesn't know that every value in baseSet is an instance of DerivedType - there has to be an execution-time check. The reverse (creating a HashSet from a HashSet) would work in C# 4. FURTHER EDIT: If you just want to use UnionWith, that doesn't require a HashSet - it requires an IEnumerable.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions