Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 675259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:47:55+00:00 2026-05-14T00:47:55+00:00

In this section of a function (.NET 2.0): public void AttachInput<T>(T input) where T

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:47:56+00:00Added an answer on May 14, 2026 at 12:47 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 363k
  • Answers 363k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Multiple fonts indicate an order of preference in fonts to… May 14, 2026 at 3:20 pm
  • Editorial Team
    Editorial Team added an answer If you check the edit history, you'll see that I… May 14, 2026 at 3:20 pm
  • Editorial Team
    Editorial Team added an answer Why don't you place the two controls (the list and… May 14, 2026 at 3:20 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.