Answers for "c# generic class"

C#
0

c# generic class

public class Car<TManufacturer, TModel,TYear>
    {
        public TManufacturer manufacturer;
        public TModel model;
        public TYear year;

        public Car(TManufacturer manufacturer, TModel model, TYear year)
        {
            this.manufacturer = manufacturer;
            this.model = model;
            this.year = year;

        }

    }
    // after we initialize it with
    public Car<string, string, int> car = new Car<string, string, int>();
//by iq18but18cm
Posted by: Guest on November-16-2021
0

c# collection of generic classes

public abstract class MyClass
{
    public abstract Type Type { get; }
}

public class MyClass<T> : MyClass
{
    public override Type Type
    {
        get { return typeof(T); }
    }

    public T Value { get; set; }
}

// VERY basic illustration of how you might construct a collection
// of MyClass<T> objects.
public class MyClassCollection
{
    private Dictionary<Type, MyClass> _dictionary;

    public MyClassCollection()
    {
        _dictionary = new Dictionary<Type, MyClass>();
    }

    public void Put<T>(MyClass<T> item)
    {
        _dictionary[typeof(T)] = item;
    }

    public MyClass<T> Get<T>()
    {
        return _dictionary[typeof(T)] as MyClass<T>;
    }
}
Posted by: Guest on July-15-2020

C# Answers by Framework

Browse Popular Code Answers by Language