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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:24:04+00:00 2026-06-16T23:24:04+00:00

I have a problem with designing my function so that it can act differently

  • 0

I have a problem with designing my function so that it can act differently for different types. My function is used to create List of objects with different types, so it wouldn’t be a problem to create several similar functions, but if it’s possible I’d like to avoid it to make my code a bit shorter:

static const int FIRST_TYPE = 0;
static const int SECOND_TYPE = 1;
static const int THIRD_TYPE = 2;

I use those ints as an argument for the function:

public void foo(int type)
{
    List<TypeIDontYetKnow> deserialized; 
    switch (type)
    {
        case FIRST_TYPE:
            deserialized = new List<A>();
            break;
        case SECOND_TYPE:
            deserialized = new List<B>();
            break;
        case THIRD_TYPE:
            deserialized = new List<C>();
            break;
    }
}

Is it possible to achieve something like this?

  • 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-16T23:24:05+00:00Added an answer on June 16, 2026 at 11:24 pm

    You need a generic method

    public void foo<T>()
    {
        List<T> deserialized = new List<T>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently encountered a problem designing a web page. I needed to have rounded
I'm re-designing a Web site and I have a problem with the existing data
I have a unique issue - I am designing a web application that creates
Programming language: Java Task: designing a hash function that maps Chinese Strings to numbers
Suppose that I have two data types Foo and Bar. Foo has fields x
I have a problem about designing my application. I have a webservice with methods.
I have this problem. I defined a function to remove a link (just for
I'm designing a library that will be used to intercept and process incoming messages
I have a function that will change a combo box selected index, so combobox_selectionchanged
I have a list of checkpoints and then a run a function. I originally

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.