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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:05:41+00:00 2026-06-11T10:05:41+00:00

I have a list of numbers, and I wrote a method that performs some

  • 0

I have a list of numbers, and I wrote a method that performs some calculations on these numbers; all in all, it’s about a page of code. The method performs some arithmetic and comparisons on these numbers.

My problem is that, in one case, the list is an IList<byte>, and in another case, it’s an IList<float>. The algorithm in both cases is exactly the same (yes, I’m aware of things like overflow errors and loss of precision, but in my case it works). How can I write a method that will handle both lists ? I can’t write something like void DoStuff<T>(IList<T> numbers), because there are no arithmetic operators (+ - * /) that are generic.

One solution is to simply store everything as float, but I’d like to avoid it. The lists are quite long, and thus storing floats instead of bytes would cost too much memory. I could also do something like DoStuffFloat(byteList.Select(b => (float)b)), but I don’t want to pay the performance penalty, either, if I can avoid it.

Short of copy-pasting the entire method and replacing “float” with “byte” (or vice versa), is there some decent solution ?

EDIT: I should’ve mentioned that I’m restricted to using .NET 3.5 for this project.

  • 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-11T10:05:42+00:00Added an answer on June 11, 2026 at 10:05 am

    I don’t know if this is the best method for your case but it is useful for similar cases too.

    This can be done by using the dynamic keyword. What dynamic will do is it will not do the compile time checks until runtime.

    Here is a small sample program to show how it works.

    class Program
    {
        static void Main()
        {
            List<byte> bytes = new List<byte>();
            bytes.Add(2);
            bytes.Add(1);
    
            List<float> floats = new List<float>();
            floats.Add(2.5F);
            floats.Add(1F);
    
            Console.WriteLine(DoStuff(bytes));
            Console.WriteLine(DoStuff(floats));
            Console.ReadLine();
        }
    
        static dynamic DoStuff(IList items)
        {
            dynamic item0 = items[0];
            dynamic item1 = items[1];
            return item0 - item1;
        }
    
    }
    

    Unfortunately in my quick testing I could not make IList<dynamic> work however using the non generic IList then accessing the members as a dynamic works fine.

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

Sidebar

Related Questions

I have a list that has some chapter numbers in string. When I sort
I have a list of divs on a page that all contain 3 divs
I have a list of numbers and need to put them all into a
I have a list of phone numbers that have been dialed (nums_dialed). I also
I have a collection of about 3500 product Serial Numbers that my software uses
I have a List with numbers, and I'd like to find the position of
I have a list of numbers, for example: list=[10,50,90,60] I want to sorte the
I have a list of numbers ( integers ) (say, from 1 to 10).
I have a list of numbers which looks like this: 1.234D+1 or 1.234D-02 .
If I have a list of numbers like this one: 10,9,8,8,9,7,6,5,4,6,7,8,11,10,12,14,16,20,30,29,28,29,27,25,20,18,15,10,8,5,4,1 I want to

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.