Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
C#
using System.Collections.Generic;

namespace ConsoleApplication2
{
    class Program
    {
        private static void Main(string[] args)
        {
            Dictionary<int, HandlerBase<TBaseClass, SBaseClass>> dictionary = new Dictionary<int, HandlerBase<TBaseClass, SBaseClass>>();
            HandlerBase<TempT, TempS> temp = new TempHandler();

            dictionary.Add(1, temp);
        }
    }

    class TBaseClass
    {

    }

    class SBaseClass
    {

    }

    class TempT : TBaseClass
    {

    }

    class TempS : SBaseClass
    {

    }


    class HandlerBase<T, S>
        where T : TBaseClass
        where S : SBaseClass
    {

    }

    ////////////////////////////////////////

    class TempHandler : HandlerBase<TempT, TempS>
    {
        public TempHandler() { }
    }


}


What I have tried:

I'm trying to take a generic T base class and used it in dictionary.
Posted
Updated 17-May-16 23:53pm
Comments
Patrice T 18-May-16 4:16am    
What is the problem ?
Use Improve question to update your question.

1 solution

HandlerBase<tbaseclass,> is not the same as HandlerBase<tempt,>, even TempT inherits TBaseClass and TempS inherits SBaseClass...
You should define dictionary to hold HandlerBase<tempt,>...

[EDIT]
To make it clear more for you...
A generic is a actually a generic type, that can have different actual types, but the very moment you actually created an instance, that instance not generic anymore...it has a very specific, well defined type, and inner inheritance not part of it (replace TBaseClass and SBaseClass with object to see it more clearly)...
What you have expected, that the different instances of the same generics behave like inherited object one from the other - wrong...

See for instance List<string> and List<int>...If you follow your logic than any member of List<string> can be converted to a member of List<int>, but that means, that any string can be converted to int - and that we know not to be true...
(The missing clue is that both int and string has the same base class)
[/EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900