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

Related Questions

I get a generic failure when I try to run: saslpasswd2 username This was
When I try this code: class MyStuff: def average(a, b, c): # Get the
When I try this code: a, b, c = (1, 2, 3) def test():
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
Using something like this: try: # Something... except Exception as excep: logger = logging.getLogger(component)
Is it better to do this: try { ... } catch (Exception ex) {
I would try something like this but it isn't allowed. function GetDynamicModulesProperties() { var
I'll try to make this as straight forward as possible. Currently our team has
I'll try to explain this better on another question. This is the query that
I'm thinking specifically about the generic class HashSet<T> . It implements several interfaces, but

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.