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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:52:58+00:00 2026-05-25T21:52:58+00:00

I created a method to organize a generic list without know the type, it

  • 0

I created a method to organize a generic list without know the type, it will sort if its int or decimal.

However the code that retrieves the values from textboxes uses List

I tried to convert it to List, but it doesnt work.
I want this code to work if they type integers or decimals or strings in the textboxes.

This was part of an interview question where they asked not to use the sort method, and that the input should receive for example INTS or DECIMALS

private void btnSort_Click(object sender, EventArgs e)
        {
            List<int> list = new List<int>();
            list.Add(int.Parse(i1.Text));
            list.Add(int.Parse(i2.Text));
            list.Add(int.Parse(i3.Text));
            list.Add(int.Parse(i4.Text));
            list.Add(int.Parse(i5.Text));
            Sort(list);
            StringBuilder sb = new StringBuilder();
            foreach (int t in list)
            {
                sb.Append(t.ToString());
                sb.AppendLine();
            }
            result.Text = sb.ToString();
        }


        private void Sort<T>(List<T> list)
        {
            bool madeChanges;
            int itemCount = list.Count;
            do
            {
                madeChanges = false;
                itemCount--;
                for (int i = 0; i < itemCount; i++)
                {
                    int result = Comparer<T>.Default.Compare(list[i], list[i + 1]);

                    if (result > 0)
                    {
                        Swap(list, i, i + 1);
                        madeChanges = true;
                    }
                }
            } while (madeChanges);
        }


        public List<T> Swap<T>(List<T> list,
                int firstIndex,
                int secondIndex)
        {
            T temp = list[firstIndex];
            list[firstIndex] = list[secondIndex];
            list[secondIndex] = temp;

            return list;
        }

I wanted that something like this: but gives error
Error 1 The type or namespace name ‘T’ could not be found (are you missing a using directive or an assembly reference?) c:\users\luis.simbios\documents\visual studio 2010\Projects\InterViewPreparation1\InterViewPreparation1\Generics\GenericsSorting1.cs 22 18 InterViewPreparation1

List list = new List();
list.Add(i1.Text);
list.Add(i2.Text);
Sort(list);

  • 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-25T21:52:59+00:00Added an answer on May 25, 2026 at 9:52 pm

    because its an interview question in which they asked not to use the
    sort method.

    In this case you can add a generic constraint IComparable<T> and then use the CompareTo() method:

     private void Sort<T>(List<T> list) where T : IComparable<T>
     {
        //...
     }
    

    Edit:

    You would have to write custom code to determine whether the input is string, int or decimal, i.e. use TryParse(..) – this will be very fragile though. Once you do know the type (one way or another) you can use MakeGenericType() and Activator.CreateInstance() to create your List<T> object at run time and then use MakeGenericMethod() to call your generic method:

    Type type = typeof(string);
    IList list = (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
    //add items to list here
    
    var p = new Program();
    MethodInfo method = typeof(Program).GetMethod("Sort");
    MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { type });
    genericMethod.Invoke(p, new [] {list} );
    

    I am pretty sure that is not what the interview question intended to ask for.

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

Sidebar

Related Questions

I created a method and pass the element type, id, and any inner text
I've created a extension method of Jquery datepicker, below are the source code. public
I've created a method that generates a new class and adds some methods into
I'm trying to learn about Expression trees, and I've created a method that takes
Given a DataSet, I'm left joining DataTables[1-n] onto DataTable[0]. I have created a method
I have created a console application that calls a method on a webservice. I
I created a Winforms AutoComplete combobox control inheriting from ComboBox with the following method:
Is there a built in method for waiting for a file to be created
I have a method running in a seperate thread. The thread is created and
I have created a .NET DLL which makes some methods COM visible. One method

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.