It is a general misconception that “using static” keyword is used to include only static classes as directive in C# 6. But I can also use Enum and non-static clasess in using directive of C# 6.
For Using Non-Static classes in using block as directive in C# 6 visit here & Using Static Classes as Directive in C# 6 visit here
Before C# 6
- using System;
- namespace EnumWithNameSpace
- {
- enum MyColors{RED = 1,ORANGE = 2,YELLOW = 3,GREEN = 4,BLUE = 5,INDIGO = 6,VIOLET = 7};
-
- class Program
- {
- static void Main(string[] args)
- {
-
- string[] colorListinCSharp5 = Enum.GetNames(typeof(MyColors));
- Enum MyCsharp5Enum1 = (MyColors)Enum.Parse(typeof(MyColors), "RED");
- Enum MyCsharp5Enum2 = (MyColors)Enum.Parse(typeof(MyColors), "1");
- Enum MyCsharp5Enum3 = (MyColors)1;
- Enum MyCsharp5Enum4 = (MyColors)Enum.ToObject(typeof(MyColors), 1);
- Enum MyCsharp5Enum5 = (MyColors)3;
-
- string myCsharp5EnumString1 = ((MyColors)1).ToString("F");
- string myCsharp5EnumString2 = ((MyColors)1).ToString("F");
- string myCsharp5EnumString3 = MyColors.BLUE.ToString("d");
- string myCsharp5EnumString4 = MyColors.BLUE.ToString("D");
- string myCsharp5EnumString5 = MyColors.BLUE.ToString("g");
- string myCsharp5EnumString6 = MyColors.BLUE.ToString("G");
- string myCsharp5EnumString7 = MyColors.BLUE.ToString("x");
- string myCsharp5EnumString8 = MyColors.BLUE.ToString("X");
-
- string myCsharp5EnumString9 = Enum.GetName(typeof(MyColors), 5);
-
- string myCsharp5FormatEnumString1 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "d");
- string myCsharp5FormatEnumString2 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "D");
- string myCsharp5FormatEnumString3 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "f");
- string myCsharp5FormatEnumString4 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "F");
- string myCsharp5FormatEnumString5 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "g");
- string myCsharp5FormatEnumString6 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "G");
- string myCsharp5FormatEnumString7 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "x");
- string myCsharp5FormatEnumString8 = Enum.Format(typeof(MyColors), MyColors.ORANGE, "X");
-
- }
- }
- }
In C# 6
- using System;
- using static MyEnumExampleforColor.MyColors;
- using static System.Enum;
-
- namespace EnumWithoutNameSpaceinCSharp6
- {
- enum MyColors { RED = 1, ORANGE = 2, YELLOW = 3, GREEN = 4, BLUE = 5, INDIGO = 6, VIOLET = 7 };
-
- class Program
- {
- static void Main(string[] args)
- {
-
- string[] colorListinCSharp6 = GetNames(typeof(MyColors));
- Enum MyCsharp6Enum1 = (MyColors)Parse(typeof(MyColors), "RED");
- Enum MyCsharp6Enum2 = (MyColors)Parse(typeof(MyColors), "1");
- Enum MyCsharp6Enum3 = (MyColors)1;
- Enum MyCsharp6Enum4 = (MyColors)ToObject(typeof(MyColors), 1);
- Enum MyCsharp6Enum5 = (MyColors)3;
- string myCsharp6EnumString1 = ((MyColors)1).ToString("F");
- string myCsharp6EnumString2 = ((MyColors)1).ToString("F");
-
- string myCsharp6EnumString3 = BLUE.ToString("d");
- string myCsharp6EnumString4 = BLUE.ToString("D");
- string myCsharp6EnumString5 = BLUE.ToString("g");
- string myCsharp6EnumString6 = BLUE.ToString("G");
- string myCsharp6EnumString7 = BLUE.ToString("x");
- string myCsharp6EnumString8 = BLUE.ToString("X");
-
- string myCsharp6EnumString9 = GetName(typeof(MyColors), 5);
-
- string myCsharp6FormatEnumString1 = Format(typeof(MyColors), ORANGE, "d");
- string myCsharp6FormatEnumString2 = Format(typeof(MyColors), ORANGE, "D");
- string myCsharp6FormatEnumString3 = Format(typeof(MyColors), ORANGE, "f");
- string myCsharp6FormatEnumString4 = Format(typeof(MyColors), ORANGE, "F");
- string myCsharp6FormatEnumString5 = Format(typeof(MyColors), ORANGE, "g");
- string myCsharp6FormatEnumString6 = Format(typeof(MyColors), ORANGE, "G");
- string myCsharp6FormatEnumString7 = Format(typeof(MyColors), ORANGE, "x");
- string myCsharp6FormatEnumString8 = Format(typeof(MyColors), ORANGE, "X");
-
- }
- }
- }
293