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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:13:52+00:00 2026-06-07T01:13:52+00:00

My program intensively uses Reverse , e.g. Array.Reverse(myArray,3,5) I would like my program could

  • 0

My program intensively uses Reverse, e.g. Array.Reverse(myArray,3,5)

I would like my program could accept both array and List as input, so I go for IList

However, I couldn’t find an IList method which is the same as Reverse.

There is one extension method named Reverse, however it produces IEnumerable stream but not in-place rearrangement. (which I assume takes more copy time)

I thought of using cast, but was afraid that cast would be inefficient as well.

So, what should I do?

Worst case scenario, I make 2 program, 1 takes array, the other takes List, and then overloading?

  • 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-07T01:13:54+00:00Added an answer on June 7, 2026 at 1:13 am

    OOP-way – make a wrapper, overload it dozen times:

    public void Reverse(Array arr, int index, int count)
    {
        Array.Reverse(arr, index, count);
    }
    public void Reverse<T>(List<T> lst, int index, int count)
    {
        lst.Reverse(index, count);
    }
    

    Add an overload each time you need another collection-alike class to be reversed in such way. This approach relies on system internals, very effective and robust, but may be verbose in case you are willing to reverse many kinds of objects.

    I-can-do-it-myself-better-way:

    static class Extensions
    {
        public static void Reverse(this IList target, int index, int count)
        {
            int right = index + count - 1;
            int left = index;
            while (right>left)
            {
                var tmp = target[left];
                target[left] = target[right];
                target[right] = tmp;
                right--;
                left++;
            }
        }
    }
    

    Just add range checks/preconditions/invariants/etc. Also, it might be inefficient with lists, as it requires random access to the contents of the list, but I think you can’t workaround it using “conventional weapons” (i.e. not using reflection and direct memory manipulation).

    So, my suggestion – overloading is the way to go.

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

Sidebar

Related Questions

Program runs once and it both throws data to the pipe and gets it
This program builds a dictionary out of a list based on what two index
The program I'm coding now makes a pretty huge list of data items. Now,
EDIT: I would like to thank you all for the swift replies ^^ Sleep()
Program I'm making has a simple configuration file looking something like this. @overlays =
I'm trying to automate input into a command line C program (which I have
I am using JDBC. I have database intensive computations, and I would like to
I hava a java program, a section of it is compute intensive, like this
I've currently written a program in C++ that sometimes uses over 300 threads. In
I wrote a simple python program that looks to me like it should be

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.