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

Feature Request: enum to act like types #3704

Closed
ghost opened this issue Jun 26, 2015 · 6 comments
Closed

Feature Request: enum to act like types #3704

ghost opened this issue Jun 26, 2015 · 6 comments

Comments

@ghost
Copy link

ghost commented Jun 26, 2015

It would be very useful if C# enums act like classes, so we can add methods within the enum's block scope:

Java:

enum DaysOfWeek{
    SUNDAY, 
    MONDAY, 
    TUESDAY, 
    WEDNESDAY,
    THURSDAY, 
    FRIDAY,
    SATURDAY;

    public boolean isWeekDay(){
        return !isWeekEnd();   
    }

    public boolean isWeekEnd(){
        return (this == SUNDAY || this == SATURDAY); 
    }

}

public class Test {
    public static void main(String[] args) throws Exception {
        DaysOfWeek sun = DaysOfWeek.SUNDAY; 
        System.out.println("Is " + sun + " a weekend? " + sun.isWeekEnd());
        System.out.println("Is " + sun + " a week day? " + sun.isWeekDay());
    }
}

C#:

public enum DaysOfWeek {
    SUNDAY, 
    MONDAY, 
    TUESDAY, 
    WEDNESDAY,
    THURSDAY, 
    FRIDAY,
    SATURDAY
}

public static class DaysOfWeekExtensions {
    public static bool isWeekDay(this DaysOfWeek day) {
        return !day.isWeekEnd();
    }

    public static bool isWeekEnd(this DaysOfWeek day) {
        return (day == DaysOfWeek.SUNDAY || day == DaysOfWeek.SATURDAY);
    }
}

public class Test {
    public static void Main(String[] args) {
        DaysOfWeek sun = DaysOfWeek.SUNDAY; 
        Console.WriteLine("Is " + sun + " a weekend? " + sun.isWeekEnd());
        Console.WriteLine("Is " + sun + " a week day? " + sun.isWeekDay());

        /* Example of how C# enums are not type safe */
        sun = (DaysOfWeek) 1999;
        Console.WriteLine(sun);
    }
}

Reference: http://www.25hoursaday.com/CsharpVsJava.html#enums (I modified the C# example with extensions)

@leppie
Copy link
Contributor

leppie commented Jun 26, 2015

This will only be useful for virtual methods (maybe properties could be nice), and the only one possibly worth changing is ToString. I dont think this adds any value.

using static also aids in typing overhead. You can for example do:

using static System.DayOfWeek;

public static class DaysOfWeekExtensions {
    // it would have been nice if using static could be scoped here    
    public static bool isWeekDay(this DayOfWeek day) {
        return !day.isWeekEnd();
    }

    public static bool isWeekEnd(this DayOfWeek day) {
        return (day == Sunday || day == Saturday);
    }
}

@whoisj
Copy link

whoisj commented Jun 27, 2015

Maybe via extension methods?

@ghost
Copy link
Author

ghost commented Jun 27, 2015

Maybe via extension methods?

#3704 (comment)

@whoisj
Copy link

whoisj commented Jun 27, 2015

@jasonwilliams200OK I was semi-agreeing, not actually asking a question. Sorry it was from a mobile, without a proper keyboard I tend to be rather terse.

I'm not sure if this is a great idea or not, but I'm willing to accept it does have useful applications, and fit within the current paradigm if done as extension methods and not as inline member methods.

@gafter
Copy link
Member

gafter commented Nov 20, 2015

The value this would add is that it eliminates the need to define a separate class of extension methods. It also allows you to define properties, which cannot be done today with extensions.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

See also #18002 for a general discussion of closing and/or moving language issues out of this repo.

This specific request is now being tracked at dotnet/csharplang#297

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants