Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseTypeof #598

Open
Hosch250 opened this issue Jun 18, 2016 · 0 comments
Open

UseTypeof #598

Hosch250 opened this issue Jun 18, 2016 · 0 comments

Comments

@Hosch250
Copy link
Contributor

Hosch250 commented Jun 18, 2016

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.

@Hosch250 Hosch250 changed the title UseTypeofOnGenericTypeParameters UseTypeof Jun 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants