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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:57:28+00:00 2026-06-09T11:57:28+00:00

In my application, I have something like: public enum Locations { LocationA, LocationB, LocationC

  • 0

In my application, I have something like:

public enum Locations {
  LocationA,
  LocationB,
  LocationC
}

private List<Locations> _myLocations;

public Int64 PackedLocations {
  get {
    return PackEnumList(this._myLocations);
  }
}

So: an enum (backed by int), a List of those enum values, and a read-only property which returns the result of a method I’ve left out so far.

That method, PackEnumList, is meant to give me a 64-bit integer where each BIT denotes whether the corresponding enum value was selected or not in a list of unique enum values. So in my example above, if _myLocations has only one item: {Locations.LocationA}, the result would be 1 (binary: …00001), if we then add Locations.LocationC to that list, the result would be 5 (binary: …000101). The implementation isn’t important right now (but I’ll include it below for completion/interest/feedback), but the signature of that method is:

public Int64 PackEnumList(List<Enum> listOfEnumValues) {
   ...
}

When I compile, I get an error that “the best overloaded method … has some invalid arguments”.

I’m guessing this is because _myLocations is being seen as a List of int values, but I’d like PackEnumList() to work even if the enumeration being used were backed by something else, if possible.

Is there a more appropriate way to make a method which will accept a List/Collection of any enumeration?

For completeness, here’s the rest of what I’m trying to do (these are static because they’re in a shared utility class). These are completely untested yet (since I can’t get past the compile error when calling the pack method), so take them with a grain of salt. And there might be a better way to do this, I’m doing this half to solve an interesting problem, and half because I think it is an interesting problem.

    public static Int64 PackEnumList(List<Enum> listOfEnumValues) {
        BitArray bits = new BitArray(64, defaultValue: false);
        foreach (var value in listOfEnumValues) {
            // get integer value of each Enum in the List:
            int val = Convert.ToInt32(value);
            if (val >= 64) {
                // this enum has more options than we have bits, so cannot pack
                throw new Exception("Enum value out of range for packing: " + val.ToString());
            }
            bits[val] = true;
        }
        var res = new Int64[1];
        bits.CopyTo(res, 0);
        return res[0];
    }

    // (this method is a little farther from the ideal: the resulting list will need
    //     to be matched by the caller to the appropriate List of Enums by casting 
    //     each Int32 value to the Enum object in the list)
    public static List<Int32> UnpackEnumList(Int64 packedValue) {
        string binaryString = Convert.ToString(packedValue, 2);
        List<Int32> res = new List<Int32>();
        for (int pos = 0; pos < binaryString.Length; pos++) {
            if (binaryString[binaryString.Length - pos - 1] == '1') {
                // bit is on
                res.Add(pos);
            }
        }
        return res;
    }
  • 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-09T11:57:30+00:00Added an answer on June 9, 2026 at 11:57 am

    Is there a more appropriate way to make a method which will accept a List/Collection of any enumeration?

    Within straight C#? Nope. But you can fudge it…

    I have a project called Unconstrained Melody which allows you to make a generic method with a constraint of “T must be an enum type” or “T must be a delegate type”. These are valid constraints at the IL level, but can’t be expressed in C#

    Basically Unconstrained Melody consists of two parts:

    • A library of useful methods with those constraints, where the source code is written using valid C# which doesn’t actually represent those constraints, but uses a marker interface
    • An IL-rewriting project (ugly but servicable) which converts those constraints into the real “unspeakable” ones

    (The expectation is that users of the library would just use the rewritten binary.)

    It sounds like you could use the latter part of the project for your code here. It won’t be terribly pleasant, but it would work. You might also find the library part useful.

    As a side thought, you might want to consider using a [Flags]-style enum instead:

    [Flags]
    public enum Locations {
      LocationA = 1 << 0,
      LocationB = 1 << 1,
      LocationC = 1 << 2
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this in my Spring Application: public class Book{ public Book(){
I have something like this: ... public partial class MyPage : System.Web.UI.Page { private
I have a Visual Studio 2008 C++ application that does something like this: template<
I have a database in my application in which I store something like a
I have WPF application and a window in it. Lets have something like this
I have something like following: public interface A { .... } public abstract class
I have something like this: enum EFood{ eMeat, eFruit }; class Food{ }; class
My application have something like this: TabActivity Tab 1 (ActivityGroup) Activity > Activity >
I have something I am trying to achieve. I have a web application running
I am willing to develop a mobile application. I wish to have something working

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.