static void Main(string[] args) { Enum one = MyEnum.One; Console.WriteLine(one); MyEnum digit = MyEnum.Zero; Enum zero = digit; Console.WriteLine(zero); Console.WriteLine(new string('-', 30)); for (MyEnum number = MyEnum.Zero; number <= MyEnum.Three; number++) { Console.WriteLine(number); Console.WriteLine((byte)number); } Console.WriteLine(new string('-', 50)); // щас компилятор принициализирует САМ константы в перечислении // он ВСЕГДА инициализирует константы СВЕРХУ ВНИЗ Т.К показано в примерах позже. // он никогда не проинициализириует константы нулями. Всегда будут нормальные числа for (MyEnum1 number = MyEnum1.Zero; number <= MyEnum1.Three; number++) { Console.WriteLine(number); Console.WriteLine((byte)number); } Console.WriteLine(new string('-', 50)); //Delay Console.ReadKey(); }
public void box_unbox_Test_1() { // Creates objects for testing of different casts. object o_enum = MyEnum.Value; object o_enum1 = MyEnum1.Value; object o_long = 24L; object o_class = new CastTestClass(); object o_guid = Guid.NewGuid(); // Try casts that shoud succeed. Any exception here means failure. // First we try casts that should succeed. // Casts between enums with the same basic type MyEnum2 e2 = (MyEnum2)o_enum; // line 2 // Cast from enum to primitive type that enum is based on short sv = (short)o_enum; Assert.Equal(sv, (short)MyEnum.Value); // Cast from enum to primitive type that enum is based on int iv = (int)o_enum1; Assert.Equal(iv, (short)MyEnum1.Value); int i_long = (int)(long)o_long; CastTestClass cls = (CastTestClass)o_class; Guid guid = (Guid)o_guid; // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { MyEnum1 e1 = (MyEnum1)o_enum; }, "Trying to cast incompatible enums - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_long; }, "Trying to cast long to int - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_class; }, "Trying to cast object to int - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_enum; }, "Trying to cast enum to int - should throw InvalidCastException"); // Now casts that should throw exception. Any cast that does not throw - means error. Assert.Throws(typeof(InvalidCastException), () => { int i = (int)o_guid; }, "Trying to cast Guid to int - should throw InvalidCastException"); }
public ClassWithStructAndEnum(Business.ClassWithStructAndEnum source) { Id = source.Id; AStruct = new MyStruct() { Field1 = source.AStruct.Field1, AnotherField2 = source.AStruct.Field2 }; AnEnum1 = (DTO.ClassWithStructAndEnum.MyEnum1)(int) source.AnEnum1; AnEnum2 = Convert(source.AnEnum2); }
public async Task TestEnumSetAndGet2() { var f = EnvironmentV2.instance.GetOrAddTempFolder("TestEnumSetAndGet"); f.DeleteV2(); IKeyValueStore store = new FileBasedKeyValueStore(f); string myKey1 = "myKey1"; MyEnum1 e1 = MyEnum1.state2; await store.Set(myKey1, e1); MyEnum1 e2 = await store.Get(myKey1, MyEnum1.state1); Assert.Equal(e1, e2); }
static void test01() { /* * IEnumerable<T> をわかった気になる * 実装するとforeach(var d in collection){ ... }ができる */ MyEnum1 e1 = new MyEnum1(); foreach (string p in e1) { Console.WriteLine(p); } Console.WriteLine("-----"); foreach (string p in e1.OrderBy(e => e)) { Console.WriteLine(p); } }
public EnumTester() { MyEnum item = new MyEnum(); item = MyEnum.PROD; if (item == MyEnum.PROD) { item = MyEnum.RELEASE; } MyEnum1 item1 = MyEnum1.TEST; if (item1.Equals(1)) { } //Cannot be true if ((Int32)item1 == 1) { } //Dynamic cast matching enum return type DataConnectionResource res = DataConnectionResource.LOCALMYSQL; String test = res.GetStringValue(); test = EnumString.GetStringValue(res); }
public static nint ToNative1(MyEnum1 value) { throw new NotImplementedException(); }