Title: templates and specialized cases?
Tags: c#
Question: let's asume that I've created the following :
public class FuelConstants<T&gt;
public FuelConstants<T&gt;() {/*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&gt;
public FuelConstants<int&gt;() {/*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
Comment: Done, I hope It's more clear now

Comment: I think you'll need to clarify your question a bit more.

Here is the accepted answer: 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&gt;
public T C_percentage;
public T H_percentage;
public T O_percentage;
public T N_percentage;
public T S_percentage;
public FuelConstants<T&gt;()
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))
...
Comment for this answer: You cannot cast directly to `T` because `int` is not a (statically known) subclass of `T`, nor is `T` a subclass of `int`. But, of course, you can cast everything to `object`, and you can cast from `object` to everything, because there's a subtyping relationship between object any any other type (unmanaged pointers excepted). At runtime it obviously works because `T` is actually `int`, so we're just boxing it and then unboxing it back. Unfortunately, while JIT is smart enough to elide the `typeof` comparison, it leaves the boxing/unboxing in, last time I checked.
Comment for this answer: might I ask for a short example of this solution please?
Comment for this answer: cheers for that, interesting read - never knew about the object cast

Here is another answer: 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.

Here is another answer: 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&gt;()
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&gt;
public MyFuelConstants()
: base()
C_percentage = 5;
H_percentage = 4;
O_percentage = 3;
N_percentage = 2;
S_percentage = 1;
// Add extra functionality here.
Comment for this answer: I'm looking to create a special case in which I'll be using a set of pre-defined values
Comment for this answer: @Kenny: Yeah. They're similar concepts, but the implementation is very different.
Comment for this answer: @Maciek: Could you give us an example please? In C++ perhaps?
Comment for this answer: @Maciek: See my new answer. Saying that, my previous one may still be somewhat relevant, so read through both if you would please.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:09/29/09', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#>', 'Title': 'templates and specialized cases?', 'ViewCount': 134, 'id': 1495001, 'user_AboutMe': '', 'user_AccountId': 47925.0, 'user_DisplayName': 'Maciek', 'user_Reputation': 18855.0, 'user_Views': 1285.0, 'user_id': 142168.0}}