-
I would like to propose a swift-type way (I think swift has this feature) to use enums without fully qualifying their name. If I want to assign a value to an enum, I can do it this way:
Then later, I might do this:
I find that there is lots of noise that is not needed. If I understood correctly (from explanations of a friend of mine who uses Swift),
This is, in my opinion, very concise and readable. There is less noise for the reader, and the code gets shorter. This is especially the case when doing switch statements or comparisons:
Or:
I would definitely prefer the second version for readability. I can make a practical example with an actual enum:
Writing this code with the current C# syntax, it looks like this.
There is just lots of uneeded noise. I would like to see this as a new syntax in C#. The type of the enum would be inferred by context. Advantages: Readability, Shorter Code Disadvantages: May not be worth it, (any others?) |
Beta Was this translation helpful? Give feedback.
Replies: 27 comments 5 replies
-
This is effectively a dupe of #354 with a slightly different syntax. |
Beta Was this translation helpful? Give feedback.
-
I searched for this and didn't find any. Thanks for linking me there. Agree that is basically a dupe. I would prefer this syntax over the in #354 proposed one, it seems more intuitive to me (and is already present in a language similar to c#). |
Beta Was this translation helpful? Give feedback.
-
I'd argue that Java is more like C# and Java already allows omitting the enum type name in very specific circumstances (namely the following): // given
public enum Visibility { VISIBLE, HIDDEN };
Visibility visibility = ...;
switch (visibility)
{
case HIDDEN:
// do stuff
break;
case VISIBLE:
// do stuff
break;
} |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I see what you mean with the Java similarities. However, I don't think we should use the same syntax. Disadvantages of using the syntax from Java is that they could aswell be variable names, which could make code harder to read sometimes. When is it an enum shortcut, when is it a variable? I personally wouldn't know. If the syntax is just used for For understanding the Java way, does Java allow this:
? |
Beta Was this translation helpful? Give feedback.
-
@Mafii do you see the big difference between Because I don't. However, it may be useful in comparisons and switch blocks. However, in this case we need some special-case type inference for only equality and only for enum types. |
Beta Was this translation helpful? Give feedback.
-
Also very useful when specifying enum values as method call arguments. |
Beta Was this translation helpful? Give feedback.
-
Is definitely better (shorter, also easier to read imo) than
Especially when it's a enum name like Visibility or Color, etc. |
Beta Was this translation helpful? Give feedback.
-
Just checked and this is possible already with static usings, so:
|
Beta Was this translation helpful? Give feedback.
-
@mungojam but wouldn't that conflict if any other locally available symbols have the same name? |
Beta Was this translation helpful? Give feedback.
-
Another is initializing arrays:
This is one of the situations where a lot of readability is gained with that change. |
Beta Was this translation helpful? Give feedback.
-
I think we can use |
Beta Was this translation helpful? Give feedback.
-
Arrays do not have their type inferred outside in. That's why you can't write, for example: SomeEnum[] a = new [] { }; Instead, the type of the anonymous array creation is determined by the element types. In your case we would not be able to determine that type. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi but you can SomeEnum[] a = { @.SomeValue }; |
Beta Was this translation helpful? Give feedback.
-
For assignments, it becomes more useful in object initializers, new C { Visibility = Visible }; The class Visible {}
new C { Visibility = .Visible }; But I don't know if that really worth to introduce new syntax. This could be considered for method arguments but in the presence of overloaded methods it becomes rather brittle and could cause binding to a different method silently. |
Beta Was this translation helpful? Give feedback.
-
We could, if we don't just depend on the target type, i.e. we look for enum members in all enums in the scope, much like how extension methods are handled. |
Beta Was this translation helpful? Give feedback.
-
I dislike the dot syntax but very much want the other syntax. |
Beta Was this translation helpful? Give feedback.
-
Personally I like to have any sign. Dot is fine but I am a bit agree with @jnm2 that it felt wrong However I don't like a naked enum like Java. It make ambiguous when there are variable with the same name Maybe |
Beta Was this translation helpful? Give feedback.
-
@Thaina it's already the syntax for using reserved names as variables. |
Beta Was this translation helpful? Give feedback.
-
@Pzixel I know but it should be avoid if possible because even it not ambiguous in compiler it still ambiguous in code |
Beta Was this translation helpful? Give feedback.
-
@Thaina no one care if it's should be avoided or not. If it's a valid syntax for existing construct you cannot reuse it, it's already reserved for something else. |
Beta Was this translation helpful? Give feedback.
-
using static MyNamespace.MyClass.MyEnum;
var test = SomeEnumValue; This works in Modern C# (one of my favorite features). Note, you have to use the full name. I always love when someone asks for a feature and the language already supports what they want in an even better way 😜 |
Beta Was this translation helpful? Give feedback.
-
@AustinBryan It's not any better way when you need to Not only that you have made the whole mess if there would be name collision. Instead of just one scope you create a chance to collide with the whole file The whole point here is to let a only specific line of code that very obvious about the enum type can write a shorthand for it without even need to |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, this isn't exactly the same. For example, say I do this: using static System.Windows.HorizontalAlignment;
using static System.Windows.VerticalAlignment; And then later do this: textbox.HorizontalAlignment = Stretch;
textbox.VerticalAlignment = Stretch; The compiler will complain that the name "Stretch" is ambiguous. It can't tell if it's from HorizontalAlignment or VerticalAlignment. I'd like the compiler to just infer which enum I'm using, based on the type of variable I'm assigning it to. Kind of like type inference with |
Beta Was this translation helpful? Give feedback.
-
👍 The C# spec jargon for this is 'target-typing.' |
Beta Was this translation helpful? Give feedback.
-
It looks as though the using System;
namespace Foo
{
public enum Color { None, Red, Green, Blue }
}
namespace Bar
{
using static Foo.Color;
public class C {
public void M(out Foo.Color c)
{
c = Red;
}
}
} So one of the original examples could be written as Visibility visibility = Visible;
...
if (visibility == Hidden)
{
... |
Beta Was this translation helpful? Give feedback.
-
That doesn't work if you have two enums that both share a member. For example, HorizontalAlignment and VerticalAlignment both have "Center" and "Stretch" members, so you'd need to disambiguate which one you're using. |
Beta Was this translation helpful? Give feedback.
-
Something like this is now championed at #2926 |
Beta Was this translation helpful? Give feedback.
Something like this is now championed at #2926