Casting generic type "as T" whilst enforcing the type of T Casting generic type "as T" whilst enforcing the type of T asp.net asp.net

Casting generic type "as T" whilst enforcing the type of T


... where T : class, ISessionManager

In case you want to use the where keyword on methods here is an example that also uses generics

    public void store<T>(T value, String key)    {        Session[key] = value;    }    public T retrieve<T>(String key) where T:class    {        return  Session[key] as T ;    }


where T : class, ISessionManager

you can go even further

where T : class, ISessionManager, new()

this will force non abstract class with parameterless ctor to be handed in as T


Read up on Constraints on Type Parameters in C#.

In this particular case, you must ensure that T is a class:

public abstract class SessionManager<T>    where T : class, ISessionManager