-1

The scope of a variable is limited to the method if I declare a variable inside that method (which is not accessible outside the method). Is it possible to define an enum in a similar way?

The code won't compile if I do this:

 void someMethod()
    { 
        enum MyEnum
         {
           val1,
           val2,
           Val3
         }
    }

What might be the reason for this? I have extended my try with struct, and the same thing is happening (code won't compile). Hence I came to a conclusion that "Named types cannot be defined inside a method".

But I don't have any justification for this conclusion. What is the reason for why it is not allow me to define named types inside the method?

Note: It is not permitted, the code won't compile. Here my question is why it is not permitted.

halfer
  • 19,824
  • 17
  • 99
  • 186
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • 1
    what I know is that you can't define enum inside a function! – Monah Mar 16 '16 at 08:02
  • This might help http://stackoverflow.com/questions/5603910/c-sharp-good-style-internal-enums-in-a-method – nagyben Mar 16 '16 at 08:03
  • @mah: Absolutely it is not permitted; i here to know why it is not permitted – sujith karivelil Mar 16 '16 at 08:03
  • 1
    @un-lucky unfortunately, as stackoverflow didn't make the decision, 'why is language feature not supported' is primarily opinion based. – Pete Kirkham Mar 16 '16 at 08:06
  • Same as any other feature - nobody had a particularly compelling use case demonstrated for it and every feature starts at -100 points. – Damien_The_Unbeliever Mar 16 '16 at 08:06
  • 1
    But you could go and vote for [Proposal: Nested local functions and type declarations](https://github.com/dotnet/roslyn/issues/259) if you wanted to. – Damien_The_Unbeliever Mar 16 '16 at 08:07
  • IMO it would be very bad if it were possible. Such methods would suffer from too much responsibility. What do you want to do with it in that method that you don't want to do in a separate class? – Silvermind Mar 16 '16 at 08:07
  • (In fact, it's been split off as a separate [Proposal: Nested local type declarations](https://github.com/dotnet/roslyn/issues/9523) since nested *functions* have now been accepted as a C# 7 feature, apparently) – Damien_The_Unbeliever Mar 16 '16 at 08:09

3 Answers3

1

You cannot declare an enum inside a function. You can define them instead in a private class.

Something like

public class Foo {
  private enum MyEnum { val1, val2, val3 }
}

If you look in the C# specs then you will find that it is defined like that:

A type-declaration is a class-declaration (Section 10.1), a struct-declaration (Section 11.1), an interface-declaration (Section 13.1), an enum-declaration (Section 14.1), or a delegate-declaration (Section 15.1).

So enum is a type and further it is said that:

A type-declaration can occur as a top-level declaration in a compilation unit or as a member declaration within a namespace, class, or struct.

Now if you want to know why it is designed like that then I guess you need to consult that to the language designers.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • I think their is no typical difference between this and normal usage; – sujith karivelil Mar 16 '16 at 08:10
  • @un-lucky:- If you are looking for the reason as to why it is designed like that then you need to ask this to the language designers. I have added the quote from MSDN as to what is designed as of now :) – Rahul Tripathi Mar 16 '16 at 09:05
1

Whatever type you are going to create inside a method will have limited scope and will not exist outside it. It doesn't make sense to create such types. For such cases you can use anonymous types.

But you can define anonymous types for your given problem.

private enum MyEnum { val1, val2, val3 }; //however this should be outside the method

public void SomeMethod ()
{
    var anonymousEnum= new { MyEnum.val1,MyEnum.val2};
}
Carbine
  • 7,849
  • 4
  • 30
  • 54
-1

Types cannot be declared inside of method. Enum should be declared in namespace, struct or class.

Rambalac
  • 2,592
  • 2
  • 15
  • 14