-
Notifications
You must be signed in to change notification settings - Fork 45
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
enum length #7
Conversation
@andrewlock this is very useful, are you considering to merge this? |
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? |
Yes, sounds perfect. :) |
I thought maybe it is useful to add a documentation comment which clarifies what this property reflects. 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 |
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? |
I usually use the number of names, when I want to create a new |
Thanks all, looks good to me (I added the xmldocs for clarity) |
In order to get the length of an enum, source generator can introduce a public constant variable what is faster.