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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:52:11+00:00 2026-06-09T10:52:11+00:00

public static object GetObject(int x) { return new object { }; } public static

  • 0
public static object GetObject(int x)
{
    return new object { };
}
public static object GetObject(string x)
{
    return new object { };
}
public static void ProcessObject<T>(T x) where T : int, string <= got error here: 
{
    object o = GetObject(x);
}

Got error “A type used as a constraint must be an interface, a non-sealed class or a type parameter.”

How can I rewrite the code to get it work without write ProcessObject(int x) and ProcessObject(string x) twice?

  • 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-06-09T10:52:12+00:00Added an answer on June 9, 2026 at 10:52 am

    So what you have now (according to accepted answer and your comments) is:

    public static void ProcessObject<T>(T x)
    {
        object o;
        if (typeof(T) == typeof(int))
            o = GetObject((int)(object)x);
        else if (typeof(T) == typeof(string))
            o = GetObject((string)(object)x);
        else
            throw new Exception();
        // do stuff with o
    }
    

    I’d recommend making public int and string overloads, but to prevent code duplication, internally call another method:

    public static void ProcessObject(int x)
    {
        ProcessObject(GetObject(x));
    }
    public static void ProcessObject(string x)
    {
        ProcessObject(GetObject(x));
    }
    private static void ProcessObject(object o)
    {
        // do stuff with o
    }
    

    This makes your public methods’ input values clear: int and string are the only acceptable types, while still not duplicating your actual logic (// do stuff with o).

    You might dislike that the two public ProcessObject methods are duplicates of each other, (on the surface anyway; under the hood, they’re calling two different GetObject overloads) but I think it’s the best option.

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

Sidebar

Related Questions

I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
public static String doCensor(String toCensor) { Object[] aobj; int l = (aobj = toCensor.toCharArray()).length;
Here is my code: public static class SerializationUtil { public static string Serialize(object obj)
How come this isn't working? private static object Lock_HandleError = new object(); public static
I have a Java function called testForNull public static void testForNull(Object obj) { if
In my utility method: public static <T> T getField(Object obj, Class c, String fieldName)
I currently have a function: public static Attribute GetAttribute(MemberInfo Member, Type AttributeType) { Object[]
The class looks as follows: public static class CacheManager { private static Dictionary<string, object>
We have the following class hierarchy: public interface IManager { object GetObject(int); } public
Say I have a method that returns a business object: public static MyObject GetObject()

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.