tags:

views:

114

answers:

2

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

+6  A: 

For type safety and avoidance of boxing, your only option is a specific overload for input of type int:

public void AttachInput(int input)
{
    Inputs.Add(input);
}

Update:

The OP has updated the question with a theory:

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.

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:

public static class OverloadTest
{
    public static void Foo(int x)
    {
        Console.WriteLine("Foo(int)");
    }

    public static void Foo(bool x)
    {
        Console.WriteLine("Foo(bool)");
    }

    public static void Foo(object x)
    {
        Console.WriteLine("Foo(object)");
    }
}

Now we write a generic method to model the one in the question:

static void CallTheRightFoo<T>(T value) where T : struct
{
    OverloadTest.Foo(value);
}

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 int as one is available.

So here's the test:

struct Test { } // in order to test a user-defined value type

static void Main(string[] args)
{
    CallTheRightFoo(1);
    CallTheRightFoo(true);
    CallTheRightFoo(new Test());
}

The output is:

Foo(object)
Foo(object)
Foo(object)

To relate this to Convert.ToInt32, from your generic code you will always be calling the overload that accepts object, and so the value will have been boxed and must then be unboxed inside Convert.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.

Daniel Earwicker
Well, I guess I'll have to do that then, thanks. Sadly, it seems to me that the only use for struct constraints is to use it combined with the default keyword, but that's just a different question
marcelomeikaru
Basically `struct` is not that useful in many ways, either as a constraint or for defining our own types. It has a theoretical performance advantage but the GC is so damn good that few apps actually notice any difference.
Daniel Earwicker
Wow, you're absolutely right. It's sad though that the only way to avoid boxing is indeed creating overloads. Thanks a lot
marcelomeikaru
+1  A: 

This works, although I will not pretend to know if it is (a) a good idea or (b) efficient.

Inputs.Add((input as int?).Value);

I tend to agree with Daniel Earwicker, though, and upvoted his answer. If you have a specific implementation related to ints, create an overload for the int and let the generic handle everything else. But I don't know what else your function may be doing, either.

edit: In an unscientific test, the method I demonstrated performed marginally worse than Convert.ToInt32. As an addition, int.TryParse on input.ToString() performs far worse. Do not use. Interestingly, the control method was an int overload, and it performed about the same as the Convert.To did, but again, this test in unscientific and I'm nowhere near as knowledgeable as others here.

static void Main()
{
    Test(100);
    Test<int>(100);
    Console.Read();
}

static void Test(int input)
{
    Stopwatch watch = new Stopwatch();

    long intTicks = 0;

    int temp = 0;

    for (int i = 0; i < 1000000; i++)
    {
        watch.Reset();
        watch.Start();
        temp = input;
        watch.Stop();
        intTicks += watch.ElapsedTicks;
    }

    Console.WriteLine(intTicks);
}

static void Test<T>(T input) where T : struct
{
    Stopwatch watch = new Stopwatch();

    long convertTicks = 0;
    long tryparseTicks = 0;
    long nullableTicks = 0;

    int temp = 0;

    for (int i = 0; i < 1000000; i++)
    {
        if (input is int)
        {
            watch.Reset();
            watch.Start();
            temp = Convert.ToInt32(input);
            watch.Stop();
            convertTicks += watch.ElapsedTicks;

            watch.Reset();
            watch.Start();
            int.TryParse(input.ToString(), out temp);
            watch.Stop();
            tryparseTicks += watch.ElapsedTicks;

            watch.Reset();
            watch.Start();
            temp = (input as int?).Value;
            watch.Stop();
            nullableTicks += watch.ElapsedTicks;
        }
    }

    Console.WriteLine(convertTicks);
    Console.WriteLine(tryparseTicks);
    Console.WriteLine(nullableTicks);
}

}

Anthony Pegram