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

  • Home
  • SEARCH
  • 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 880983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:11:18+00:00 2026-05-15T12:11:18+00:00

I’d like to use a GetBytes extension method on the List class… public static

  • 0

I’d like to use a GetBytes extension method on the List class…

public static class Extensions
{
    public static byte[] GetBytes<T>(this ICollection<T> col)
    {
        List<byte> bytes = new List<byte>();
        foreach (T t in col)
            bytes.AddRange(BitConverter.GetBytes(t));

        return bytes.ToArray();
    }
}

When I compile, I am receiving a compiler error stating that Argument ‘1’: cannot convert from ‘T’ to ‘double’. Can anyone explain to me what the issue is?

  • 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-15T12:11:19+00:00Added an answer on May 15, 2026 at 12:11 pm

    BitConverter doesn’t have a GetBytes implementation that works on arbitrary types. You must pass an argument of the correct type, such as double, int, etc.

    The compiler is not finding an appropriate overload, and “defaulting” to the double overload, then reporting it as a compiler error.


    In order to use this on a general type, you’ll need to also pass in a Func that converts to bytes. This would work:

    public static class Extensions
    {
        public static byte[] GetBytes<T>(this IEnumerable<T> col, Func<T, byte[]> conversion)
        {
            return col.SelectMany(conversion).ToArray();
        }
    }
    

    You could then use this like:

    static void Main(string[] args)
    {
        double[] test = new double[3]
                            {
                                0, 12, 14.12
                            };
        List<int> ilist = new List<int>() { 3,5,1 };
    
        byte[] doubles = test.GetBytes(BitConverter.GetBytes);
        byte[] ints = ilist.GetBytes(BitConverter.GetBytes);
    
    
        Console.WriteLine("Double collection as bytes:");
        foreach (var d in doubles)
        {
            Console.WriteLine(d);
        }
    
        Console.WriteLine("Int collection as bytes:");
        foreach (var i in ints)
        {
            Console.WriteLine(i);
        }           
    
        Console.ReadKey();
    }
    

    Unfortunately, the “BitConverter.GetBytes” line (or some other function to do this conversion) makes the API call a little less “pretty”, but it does work correctly. This could be removed for specific types of T by adding overloads, such as:

    public static class Extensions
    {
        public static byte[] GetBytes(this IEnumerable<double> col)
        {
            return GetBytes<double>(col, BitConverter.GetBytes);
        }
    
        public static byte[] GetBytes(this IEnumerable<int> col)
        {
            return GetBytes<int>(col, BitConverter.GetBytes);
        }
        // Add others as needed
    
        public static byte[] GetBytes<T>(this IEnumerable<T> col, Func<T, byte[]> conversion)
        {
            return col.SelectMany(conversion).ToArray();
        }
    }
    

    Edit: Since you need to avoid SelectMany, you can just write the main function the same way you did previously. SelectMany just makes this simpler:

    public static class Extensions
    {
        public static byte[] GetBytes<T>(this IEnumerable<T> col, Func<T, byte[]> conversion)
        {
            List<byte> bytes = new List<byte>();
            foreach (T t in col)
                bytes.AddRange(conversion(t));
    
            return bytes.ToArray();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 425k
  • Answers 425k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer RMI is not a complex thing, it's the opposite. It… May 15, 2026 at 12:11 pm
  • Editorial Team
    Editorial Team added an answer $('a').each(function() { this.href += (/\?/.test(this.href) ? '&' : '?') +… May 15, 2026 at 12:11 pm
  • Editorial Team
    Editorial Team added an answer You're .post in your CSS you have the following text-align:justify;… May 15, 2026 at 12:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.