-1

I have a dictionary object like :-

var gridContent = new Dictionary<T, KeyValuePair<int, bool>>();

Now I want to typeCast this into :-

Dictionary<SomeClass, KeyValuePair<int, bool>>();

Is there any way to do so ?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Bhupesh
  • 35
  • 7
  • 3
    Can you show a more complete example of what you're trying to do, in context? – T2PS Aug 29 '18 at 09:43
  • If it *is* a `Dictionary>` then you can just cast it as you would do any other cast. – Jon Skeet Aug 29 '18 at 09:45
  • unfortunately you cannot simply cast this: `error CS0030: Cannot convert type 'System.Collections.Generic.Dictionary>' to 'System.Collections.Generic.Dictionary>'` – Chris Aug 29 '18 at 09:53
  • Why you want to do that? You are in a generic class, why should it have to know the type `SomeClass` at all? – Tim Schmelter Aug 29 '18 at 09:55
  • I need to highlight the objects which are selected on Canvas using mouse pointer on the Grid. Now these newly added object are of say type "SomeClass" and as per the previous implementation the Index of those highlighted objects are stored in the dictionary. At some places I have type information and at some the methods are generic so I need to perform operation for specific type only. – Bhupesh Aug 29 '18 at 10:05
  • 2
    The point of a generic member is that it works for *every* type, not just a specific one. So if you only have dictionaries with `SomeClass`-elements as key, why not use a `Dictionary`? Please provide a more meaningful example of what you want to achieve, preferably in form of a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – MakePeaceGreatAgain Aug 29 '18 at 10:22

1 Answers1

0

Im sure there is a way to cast this in a different way, but the first thing that came to my mind is to convert it using Linq:

PS> scriptcs
scriptcs (ctrl-c to exit or :help for help)

> public class SomeClass {}
> public class SomeClassB : SomeClass {}
> public class Test<T> where T: SomeClass
* {
*     public Dictionary<T, KeyValuePair<int, bool>> gridContent = new Dictionary<T, KeyValuePair<int, bool>>();
* }
>
> var testInstance = new Test<SomeClassB>();
> // casting error
> // var testGridContent = (Dictionary<SomeClass, KeyValuePair<int, bool>>)testInstance.gridContent;
>
> var testGridContent = testInstance.gridContent.ToDictionary(kvp=>(SomeClass)kvp.Key, kvp=>kvp.Value);
Chris
  • 527
  • 3
  • 15
  • case insensitive? i don't see any strings involved. what do you mean? – Chris Aug 29 '18 at 10:23
  • This could have issues if a custom equality comparer is used - https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.-ctor?view=netframework-4.7.2#System_Collections_Generic_Dictionary_2__ctor_System_Collections_Generic_IEqualityComparer__0__ (since `ToDictionary` will lose that). – mjwills Aug 29 '18 at 10:24
  • Yeah, its not the same dictionary. But in the constructor he uses, there is no hint of a IEqualityComparer being passed. So it should work just fine. – Chris Aug 29 '18 at 10:30
  • Also he could use another overload of the ToDictionary method and pass the same equality comparer: https://msdn.microsoft.com/en-us/library/bb548554(v=vs.110).aspx – Chris Aug 29 '18 at 10:35