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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:56:26+00:00 2026-05-30T18:56:26+00:00

I have a the following generic methods: public static string Glue(string prefix, string value)

  • 0

I have a the following generic methods:

public static string Glue(string prefix, string value)
{
    return String.Format("{0}={1}&", prefix, value);
}

public static string Format<T>(string prefix, T obj) where T : struct
{
    return Glue(prefix, (obj).ToString()); ;
}

public static string Format<T>(string prefix, List<T> obj) where T : struct
{
    return String.Join("",obj.Select(e => Glue(prefix, e.ToString())).ToArray());
}

Now I’d like to call them with a parameter that comes in as an object and could be a variety of types.

I started writing some code and it started looking like it’s going to have a very long if/else sequence:

// type of value is object, and newPrefix is string
if (value is int)
{
    return Format(newPrefix, (int)(value));
}
else if (value is double)
{
    return Format(newPrefix, (double)value);
}
...

Is there a way of avoiding this long sequence of if/else?

  • 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-30T18:56:28+00:00Added an answer on May 30, 2026 at 6:56 pm

    As stated there’s not much of a way to make this simpler. The Format method is constrained to only taking value types (structs) which is easy to detect at the call site

    if (value.GetType().IsValueType) {
      // it's a struct
    }
    

    But there’s no way to then to make the Format call happy as you can’t provide the T type.

    What you can do here is change Format slightly. The method call only uses the ToString method which is available on all types. You could remove the struct constraint and then call it with the value already in object form

    public static string Format(string prefix, object obj) {
        return Glue(prefix, obj.ToString()); ;
    }
    
    if (value.GetType().IsValueType) {
      Format(newPrefix, value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some generic types, like the following: public struct Tuple<T1, T2> { ...
I'm writing some Enum functionality, and have the following: public static T ConvertStringToEnumValue<T>(string valueToConvert,
I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following generic question Here is a my generic message interface. public
I have an extension generic method public static void AddError<TModel>( this ModelStateDictionary modelState, Expression<Func<TModel,
I have the following bogus code, but the idea is to return a generic
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
I have the following method that is supposed to be a generic Save to
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)

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.