How to get C # Enum in an interface like C?

I use for listing in C, still a little confused about how they are handled in C #. I want to implement an interface in C # that looks like

enum foo { one, two } interface Ibar { enum foo {get;} } 

where the class implementing the Ibar interface will return either foo.one or foo.two when the foo property is received. So for example

 class bar : Ibar { enum foo { get { return foo.one; } } } 

and i can do

 bar b = new bar; if (b.foo == foo.one) {... } 

The above interface code expects me to define enum inside the interface (it says that ';' should be ',').

The closest message I could find was relevant http://bytes.com/topic/c-sharp/answers/524824-enum-property-interface , but the enumeration is defined inside the class.

+4
source share
2 answers

I think the syntax you want looks like this:

 enum Foo { One, Two } interface IBar { Foo Foo { get; } } 

The result is an interface with a property named Foo type Foo .

+4
source

The syntax you want is:

 enum foo { one, two } interface Ibar { foo foo { get; } } class bar : Ibar { foo foo { get { return foo.one; } } } 

You use only enum to define a new enumeration; when creating a member variable, you use the name of the enum you created.

However, you would be much better off not calling the member variable the same as the enum type:

 enum FooEnum { One, Two } interface IBar { FooEnum FooValue { get; } } class Bar : IBar { FooEnum FooValue { get { return FooEnum.one; } } } 

(Note. I used only enum and Value to figure out what type of enum and which is the value, do not do this.)

(Also, I think this is more like regular C # usage conventions.)

+1
source

Source: https://habr.com/ru/post/1445013/


All Articles