Vannevelj / VSDiagnostics

A collection of static analyzers based on Roslyn that integrate with VS
GNU General Public License v2.0
65 stars 16 forks source link

UseTypeof #598

Open Hosch250 opened 8 years ago

Hosch250 commented 8 years ago

In this instance, you can use both typeof and GetType() to get the type of the parameter:

public class Foo<T>
{
    private void Bar<TR>(T test1, TR test2)
    {
        var t1 = test1.GetType();
        var t2 = test2.GetType();

        var i = 5;
        var ti = i.GetType();
    }
}

That should be:

public class Foo<T>
{
    private void Bar<TR>(T test1, TR test2)
    {
        var t1 = typeof(T);
        var t2 = typeof(TR);

        var i = 5;
        var ti = typeof(int);
    }
}

The reasoning being that if the generic is a value type, calling GetType() results in an unnecessary boxing of the value.