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

  • Home
  • SEARCH
  • 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 50159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:32:59+00:00 2026-05-10T16:32:59+00:00

When I try this with a generic class where this.value is T: if (this.value.GetType()

  • 0

When I try this with a generic class where this.value is T:

if (this.value.GetType() == typeof(int)) {     ((int)this.value)++; } else {     throw new InvalidOperationException             ('T must be an int to perform this operation'); } 

I get a compile-time error: ‘Cannot convert type ‘T’ to ‘int”

What should I do to perform an integral operation on this.value when it’s an int?

Note that this is just an example. The code does type conversions with generics, and ‘int’ is just an example of one type for T.

  • 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. 2026-05-10T16:32:59+00:00Added an answer on May 10, 2026 at 4:32 pm

    Unfortunately, it is very hard to convince the compiler about specific T implementations. One (nasty) approach is to cast to object in the middle (note this will box and unbox value-types):

    int i = (int)(object)this.value; i++; this.value = (T)(object)i; 

    Ugly but it works. In .NET 3.5 I have some better wrappers for generic arithmetic, here. The Operator class is part of MiscUtil; at the simplest level, I suspect AddAlternative would work very well:

    this.value = Operator.AddAlternative(this.value, 1); 

    This should infer the implicit <T,int> automatically, or you can add them yourself:

    this.value = Operator.AddAlternative<T,int>(this.value, 1); 

    Benefit: This is preferable to the original code as it doesn’t actually care about the original T – it will work for any type (even your own) that supports "T +(T,int)".

    I think there is also a ChangeType hiding around somewhere in there…

    [edit] Collin K and others make a valid remark about the architectural implications – but being pragmatic there are times when the T really does matter that much… but I’d agree with avoiding this type of specialization unless really necessary. That said (as per my comment on Collin’s post), the ability to perform things like basic arithmetic (increment, Int32 division, etc) on (for example) a Matrix<T> [for T in decimal/float/int/double/etc] is often highly valuable.

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

Sidebar

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • 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 If that is all you need, you don't need a… May 11, 2026 at 4:41 pm
  • Editorial Team
    Editorial Team added an answer It should probably not be up to the service to… May 11, 2026 at 4:41 pm
  • Editorial Team
    Editorial Team added an answer At no point in the above code is anything dereferenced.… May 11, 2026 at 4:41 pm

Related Questions

Consider the console application below, featuring a method with a generic catch handler that
I have a new ASP.NET MVC project (my first), and I had been running
I have a number of extension methods defined in my current project. It is
Has anyone else run into this problem before? I've got a method that calls

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.