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 990299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:55:38+00:00 2026-05-16T05:55:38+00:00

Is it possible to constrain a generic method on specific types? I want to

  • 0

Is it possible to constrain a generic method on specific types?

I want to write something like this:

public T GetValue<T>(string _attributeValue) where T : float, string
{
    return default(T); // do some other stuff in reality
}

I’m mostly just trying to avoid having a giant switch statement inside the method or having to throw an exception if an invalid type is specified.

Edit: Ack. I knew string is not a value type. I started out with two numeric types earlier. Sorry.

  • 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-16T05:55:39+00:00Added an answer on May 16, 2026 at 5:55 am

    You can’t use generic constraints to express the limitations you are interested in. Generics are not meant to express variation based on disjoint types – they’re meant to express variation that is unified over a hierarchy of types (or those implementing certain interfaces).

    You have a few alternative choices, however. Which you choose depends on the exact nature of what you’re trying to do.

    Use differently named methods to express each operation. I tend to use this approach when each method is truly doing something different. You could argue that returning a different type of value from a method is essentially a different operation, and deserves its own unique name.

    float GetFloat(string attrName) { }
    string GetString(string attrName) { }
    

    Provide a “default value” to allow the type to be inferred. In many designs where you ask for a value by name it useful to supply a default value. This can allow you to employ overloading to differentiate between which method to invoke (based on the type of the default value). Unfortunately, this approach is quite fragile – and breaks easily when passing literal values to overloads that accept numeric primitives (int vs. uint vs. long).

    float GetValue(string attrName, float defaultValue) { ... }
    string GetValue(string attrName, string defaultValue) { ... }
    

    Use a generic method, but throw a runtime exception if the type isn’t one of those you support. Personally I find this kind of ugly and in violation of the spirit of generics – generics should unify functionality over a hierarchy or a set of types implementing some interface. However, in some cases it makes sense to do so (if let’s so one specific type cannot be supported, let’s say). Another problem with this approach is that the signature of the generic method cannot be inferred from any parameters, so you would have to specify the type desired when calling it … at which point it’s not much better (from a syntax point of view) than having different method names.

    T GetValue<T>( string attrName )
    {
       if( typeof(T) != typeof(string) ||
           typeof(T) != typeof(float) )
           throw new NotSupportedException();
       return default(T); 
    }
    
    // call it by specifying the type expected...
    float f = GetValue<float>(attrName);
    string s = GetValue<string>(attrName);
    

    Use an out parameter instead of a return value. This approach works well, but it loses the concise syntax of being able to call a method and act on a return value, since you first have to declare a variable to populate.

    void GetValue( string attrName, out float value )
    void GetValue( string attrName, out string value )
    
    // example of usage:
    float f;
    GetValue( attrName, out f );
    string s;
    GetValue( attrName, out s );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.