Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum length #7

Merged
merged 4 commits into from
May 14, 2022
Merged

enum length #7

merged 4 commits into from
May 14, 2022

Conversation

tothalexlaszlo
Copy link
Contributor

In order to get the length of an enum, source generator can introduce a public constant variable what is faster.

Method Mean Error StdDev Ratio Gen 0 Allocated
EnumLength 41.9690 ns 0.2502 ns 0.2218 ns 1.000 0.0076 48 B
EnumLengthExtensions 9.0046 ns 0.0649 ns 0.0607 ns 0.214 0.0076 48 B
EnumLengthProperty 0.0214 ns 0.0046 ns 0.0043 ns 0.001 - -

@adamradocz
Copy link
Contributor

@andrewlock this is very useful, are you considering to merge this?

@andrewlock
Copy link
Owner

Thanks @tothalexlaszlo this looks great! Yep @adamradocz definitely going to merge this 🙂 I'm thinking it would probably would sense to merge #13 first given the additional complexity, and rebase this one on top, sound good to you?

@adamradocz
Copy link
Contributor

Yes, sounds perfect. :)

@FroggieFrog
Copy link

I thought maybe it is useful to add a documentation comment which clarifies what this property reflects.
Because it is a non distinct count of the names (if I'm correct).
A user could have an enum defined with different names, but same values.

using System;
using System.Linq;

public class Program
{
    public enum Foo
    {
        First = 0,
        Second = 1,
        Third = 1,
    }

    public static void Main()
    {
        var nameCount = Enum.GetNames(typeof(Foo)).Length;
        var valueCount = Enum.GetValues(typeof(Foo)).Length;
        var distinctNameCount = Enum.GetNames(typeof(Foo)).Distinct().Count();
        var distinctValueCount = ((Foo[])Enum.GetValues(typeof(Foo))).Distinct().Count();

        Console.WriteLine($"{nameof(nameCount)}: {nameCount}");
        Console.WriteLine($"{nameof(valueCount)}: {valueCount}");
        Console.WriteLine($"{nameof(distinctNameCount)}: {distinctNameCount}");
        Console.WriteLine($"{nameof(distinctValueCount)}: {distinctValueCount}");
    }
}
nameCount: 3
valueCount: 3
distinctNameCount: 3
distinctValueCount: 2

@andrewlock
Copy link
Owner

That's a good point. I guess one question I have is: when would you use this 🤔 i.e. when would you want to get the number of names/values but not get the actual name/values themselves?

@adamradocz
Copy link
Contributor

I usually use the number of names, when I want to create a new List<T> with an initial size of the enum.
I can't think of anything right now, when would I use the distinct value count.

@andrewlock andrewlock merged commit c19de08 into andrewlock:main May 14, 2022
@andrewlock
Copy link
Owner

Thanks all, looks good to me (I added the xmldocs for clarity)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants