In this section of a function (.NET 2.0):
public void AttachInput<T>(T input) where T : struct
{
if (input is int)
{
Inputs.Add((int)input);
}
}
The compiler shows the error “Cannot convert type ‘T’ to ‘int’.
So, I used Convert.ToInt32() which worked – but does it box input to an object? Is there a better solution? Thanks
Edit1: Taken off unnecessary stuff related to the question
Edit2: Taking a deeper look in generics, it seems to me that Convert.ToInt32 when T is already an int does no boxing to an object and its int overload is the one being called.
If that’s true, the Convert methods seem to me to be the best option at the moment
For type safety and avoidance of boxing, your only option is a specific overload for input of type
int:Update:
The OP has updated the question with a theory:
This is not right, and a simple test can demonstrate it. Suppose we write our own set of overloads just to detect which one is being called:
Now we write a generic method to model the one in the question:
The theory is that because the CLR/JIT will produce a specific version of the method for each value type, it can pick the specific overload for
intas one is available.So here’s the test:
The output is:
To relate this to
Convert.ToInt32, from your generic code you will always be calling the overload that acceptsobject, and so the value will have been boxed and must then be unboxed insideConvert.ToInt32(which also has to check what type it is first).Although to reiterate an important point behind all this: although boxing/unboxing is more expensive compared with not doing it, it is quite possibly of negligible cost compared with any actual work your code is doing. So I wouldn’t worry too much about the cost of it until you can prove that it is a genuine burden on a realistic model of an application using your library.