Define Enumeration inside Main() gives compile Error ( c#) -
define enumertion within main() gives compile error,why ? (if define in 'class program, working fine)
using system; class programme { // if define here, works fine. // public enum days {sunday,monday,tuesday,wednesday,thursday,friday,saturday}; static void main() { public enum days {sunday,monday,tuesday,wednesday,thursday,friday,saturday} days today; today = days.saturday; console.writeline("today {0}",today); } }
compile error:
d:\myprogs>csc _19enum.cs microsoft (r) visual c# 2010 compiler version 4.0.30319.1 copyright (c) microsoft corporation. rights reserved. _19enum.cs(7,5): error cs1513: } expected _19enum.cs(10,15): error cs1519: invalid token '=' in class, struct, or interface fellow member declaration _19enum.cs(10,30): error cs1519: invalid token ';' in class, struct, or interface fellow member declaration _19enum.cs(12,20): error cs1519: invalid token '(' in class, struct, or interface fellow member declaration _19enum.cs(12,41): error cs1519: invalid token ')' in class, struct, or interface fellow member declaration _19enum.cs(15,1): error cs1022: type or namespace definition, or end-of-file expected
you can't define enums within method. define them in class, or preferably outside class if going utilize them elsewhere:
using system; public enum days {sunday,monday,tuesday,wednesday,thursday,friday,saturday} class programme { // here fine static void main() { days today; today = days.saturday; console.writeline("today {0}",today); } }
c# enumeration
No comments:
Post a Comment