Maciek
Maciek

Reputation: 19893

templates and specialized cases?

let's asume that I've created the following :

public class FuelConstants<T>
{
    public FuelConstants<T>() {/*do stuff here*/}
    public T C_percentage;
    public T H_percentage;
    public T O_percentage;
    public T N_percentage;
    public T S_percentage;
};

I'd like to use this template to create a set of data for future reference/usage.

How do I create a specialized template for let's say ... ints ? Please note that I'm looking to create a specialized case for instance :

public class FuelConstants<int>
{
        public FuelConstants<int>() {/*do stuff here*/}
        public int C_percentage = 5;
        public int H_percentage = 4;
        public int O_percentage = 3;
        public int N_percentage = 2;
        public int S_percentage = 1;
};

so that depending on the T's type I'd be using a set of pre-defined values

Upvotes: 0

Views: 145

Answers (3)

Pavel Minaev
Pavel Minaev

Reputation: 101565

If you mean the ability to provide a special implementation of class for some particular T - such as what C++ allows - then there's no way to do it in C#. The closest you can come is to use if (typeof(T) == ...) checks in your methods, which will usually be optimized away by JIT:

public class FuelConstants<T>
{
    public T C_percentage;
    public T H_percentage;
    public T O_percentage;
    public T N_percentage;
    public T S_percentage;

    public FuelConstants<T>()
    {
        if (typeof(T) == typeof(int))
        {
            // cast via object is needed because from compiler's POV,
            // T and int are still unrelated types...
            C_percentage = (T)(object)123;
            H_percentage = (T)(object)456;
        }
        else if (typeof(T) == typeof(decimal))
        ...
    }
};

Upvotes: 2

Noldorin
Noldorin

Reputation: 147290

What you want to do cannot be done at compile time. The best you can accomplish is to perform a type check in the constructor, and assign values there.

public FuelConstants<T>()
{
    if (typeof(T) == typeof(int))
    {
        C_percentage = 5;
        H_percentage = 4;
        O_percentage = 3;
        N_percentage = 2;
        S_percentage = 1;
    }
    else if (typeof(T) == typeof(double))
    {
        C_percentage = 5.0;
        H_percentage = 4.0;
        O_percentage = 3.0;
        N_percentage = 2.0;
        S_percentage = 1.0;
    }
    // etc.
}

As a side point, what you have actually declared is a generic class, not a template, though there are similarities. (Templates are a feature of C++ and work somewhat differently to the arguably more powerful generic system of .NET, since the CLR/Virtual Machine handles things rather than the compiler.)

Previous Answer:

Not completely sure what you are looking for here.

If you want to create a class that derives from this generic class, and uses the int type, you can do something like the following:

public class MyFuelConstants : FuelConstants<int>
{
    public MyFuelConstants()
        : base()
    {
        C_percentage = 5;
        H_percentage = 4;
        O_percentage = 3;
        N_percentage = 2;
        S_percentage = 1;
    }

    // Add extra functionality here.
}

Upvotes: 1

Scott DePouw
Scott DePouw

Reputation: 3899

Template Specialization does not exist in C#, primarily because generics and templates are not the same. The best way to do what you are attempting to do is, as suggested in another answer, manually check the type of T.

Upvotes: 1

Related Questions