Пример #1
0
        public FunWithEnums()
        {
            Console.WriteLine("**** Fun with Enums *****");
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            // Prints out "emp is a Contractor".
            Console.WriteLine("emp is a {0}.", emp.ToString());

            // Prints out "Contractor = 100".
            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);

            EmpType e2 = EmpType.Contractor;

            // These types are enums in the System namespace.
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);

            Console.ReadLine();
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Enums *****");
            //создаётся экземпляр перечисления
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            // выводит "emp is a Contractor".
            Console.WriteLine("emp is a {0}.", emp.ToString());

            // выводит "Contractor = 100".
            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);

            EmpType e2 = EmpType.Contractor;

            // Эти перечисления объявлены в пространстве имен System
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);

            Console.ReadLine();
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with enums*****\n");
            //Make an EmpType variable.
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);
            Console.ReadLine();

            //Print storage for the enum
            Console.WriteLine("EmpType uses a {0} for storage.", Enum.GetUnderlyingType(emp.GetType()));
            Console.ReadLine();

            //This time use typeof to extract a Type.
            Console.WriteLine("EmpType uses a {0} for storage.", Enum.GetUnderlyingType(typeof(EmpType)));

            Console.WriteLine("Emp is a {0}", emp.ToString());
            Console.ReadLine();

            //Prints out Contractor = 100
            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);
            Console.ReadLine();
            EmpType e2 = EmpType.Contractor;

            //These type are enums in the System namespace.
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.DarkBlue;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);
        }
Пример #4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Enums...");
            //contractor type

            EmpType emp = EmpType.Contractor;            //emp --> Contractor

            AskForBonus(emp);

            Console.WriteLine("EmpType uses a {0} for storage\n", Enum.GetUnderlyingType(emp.GetType()));

            Console.WriteLine("emp is AccessViolationException {0} for storage\n", Enum.GetUnderlyingType(typeof(EmpType)));

            Console.WriteLine("emp is a {0}\n", emp.ToString());

            Console.WriteLine("{0} = {1}\n", emp.ToString(), (byte)emp);

            EmpType e2 = EmpType.Contractor;

            DayOfWeek    day   = DayOfWeek.Monday;
            ConsoleColor color = ConsoleColor.DarkGray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(color);
            Console.ReadLine();
        }
Пример #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("*** Fun with Enums ***\n");
            // Создать переменную типа EmpType
            EmpType emp = EmpType.Contractor; // !!! Перед значением нужно указывать имя перечисления
            AskForBonus(emp);

            // Показать тип перечисления (=> FunWithEnums.EmpType)
            Console.WriteLine("GetType() = {0}", emp.GetType()); // для переменной
            Console.WriteLine("typeof() = {0}", typeof(EmpType));// для самого перечисления


            // Вывести текущее строковое имя перечисления
            Console.WriteLine("emp is a {0}.", emp.ToString());
            // Вывести не имя, а значение заданной переменной перечисления => нужно привести ее к текущему типу хранения
            Console.WriteLine("{0} = {1}", emp.ToString(), (int)emp);
            // Вывести тип хранилища для значений перечисления.
            Console.WriteLine("EmpType uses a {0} for storage(using Enum.GetUnderlyingType())",
                Enum.GetUnderlyingType(emp.GetType())); 
            //На этот раз для получения информации о типе используется операция typeof.
            Console.WriteLine("EmpType uses a {0} for storage(using typeof)",
            Enum.GetUnderlyingType(typeof(EmpType)));

            // Enum’s Name-Value Pairs
            EmpType e2 = EmpType.Grunt;
            // Эти типы являются перечислениями из пространства имен System.
            DayOfWeek day = DayOfWeek.Monday;
            ConsoleColor cc = ConsoleColor.Gray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);

            Console.ReadLine();
        }
Пример #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Enums*****");
            //创建职员的类型
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);
            Console.WriteLine();

            //输出枚举的存储
            Console.WriteLine("EmpType uses a {0} for storage.", Enum.GetUnderlyingType(typeof(EmpType)));
            Console.WriteLine();

            //输出"emp is a Contractor"
            Console.WriteLine("emp is a {0}", emp.ToString());
            Console.WriteLine();

            //输出"Contractor=100"
            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);
            Console.WriteLine();


            EmpType      emp_2 = EmpType.Contractor;
            DayOfWeek    day   = DayOfWeek.Monday;
            ConsoleColor cc    = ConsoleColor.Gray;

            EnvaluateEnum(emp_2);
            EnvaluateEnum(day);
            EnvaluateEnum(cc);

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Enums *****");

            // Make an EmpType variable.
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            // Print storage for the enum.
            //Console.WriteLine("EmpType uses a {0} for storage",
            //    Enum.GetUnderlyingType(emp.GetType()));
            Console.WriteLine("EmpType uses a {0} for storage",
                              Enum.GetUnderlyingType(typeof(EmpType)));

            // Prints out "emp is a Contractor".
            Console.WriteLine("emp is a {0}", emp.ToString());

            // Prints out "Contractor = 100".
            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);

            // These types are enums in the System namespace.
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(emp);
            EvaluateEnum(day);
            EvaluateEnum(cc);

            Console.ReadLine();
        }
Пример #8
0
        } // end Parameterized constructor

        /// <summary>
        /// To override the ToString method so object property values are returned
        /// </summary>
        /// <returns>The string containing the object property values</returns>
        public override string ToString()
        {
            return "\r\n" + FirstName.ToString() + " " + LastName.ToString() + ":\r\n\t" +
                "Type: " + EmpType.ToString().ToLowerInvariant() + "\r\n\t" +
                "ID: " + EmpID.ToString() + "\r\n\t" +
                "Hourly Rate: " + HourlyRate.ToString("C") + "\n";
        } // end method ToString()
Пример #9
0
        static void Enums()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            Console.WriteLine("EmpType uses a {0} for storage.", Enum.GetUnderlyingType(emp.GetType()));
            Console.WriteLine("emp: Type (metadata description) is {0} / {1}",
                              emp.GetType(), typeof(EmpType));
            Console.WriteLine("emp: name is {0} (string name), value is {1}",
                              emp.ToString(), (byte)emp);
            Console.WriteLine("The value of {0} is {1} / {2}h", emp.ToString(),
                              Enum.Format(typeof(EmpType), emp, "d"), Enum.Format(typeof(EmpType), emp, "x"));
            Console.WriteLine();
        }
Пример #10
0
        } // end Parameterized constructor

        /// <summary>
        /// To override the ToString method so object property values are returned
        /// </summary>
        /// <returns>The string containing the object property values</returns>
        public override string ToString()
        {
            return "\r\n" + FirstName.ToString() + " " + LastName.ToString() + ":\r\n\t" +
                "Type: " + EmpType.ToString().ToLowerInvariant() + "\r\n\t" +
                "ID: " + EmpID.ToString() + "\r\n\t" +
                "Monthly Salary: " + MonthlySalary.ToString("C") + "\n";
        } // end method ToString()
Пример #11
0
        static void Main(string[] args)
        {
            EmpType emp = EmpType.contractor;

            AskForBonus(emp);
            Console.WriteLine($"{emp.ToString()}");
        }
Пример #12
0
        private static void FuckWithEnum_pair()
        {
            System.Console.WriteLine("***** F**k With Enums *****");

            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            System.Console.WriteLine($"{emp.ToString()} = {(int)emp}");
        }
Пример #13
0
        public void enum_usage()
        {
            // 1. use as variable
            EmpType emp = EmpType.Contractor;

            // 2. get type
            Console.WriteLine(emp.GetType().Name);
            Console.WriteLine(emp.ToString());            // output "Contractor"
            // 3. Enum.GetValues
            Array enumData = Enum.GetValues(emp.GetType());
        }
Пример #14
0
        static void Main(string[] args)
        {
            EmpType e2 = EmpType.Contractor;

            Console.WriteLine(e2.ToString());
            byte a = (byte)e2;

            Console.WriteLine(a);
            AskForBonus(e2);
            Console.ReadLine();
        }
Пример #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("=> Fun with Enums");
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);
            //using GetType method
            Console.WriteLine("EmpType uses {0} for storage", Enum.GetUnderlyingType(emp.GetType()));
            //using typeof method
            Console.WriteLine("EmpType uses {0} for storage", Enum.GetUnderlyingType(typeof(EmpType)));

            Console.WriteLine("Emp is a {0} ", emp.ToString());

            //print out enum name and value
            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(day);
            Console.ReadKey();
        }
Пример #16
0
        static void Main(string[] args)
        {
            EmpType e = EmpType.Contractor;

            Console.WriteLine(e.ToString());
            byte a = (byte)e;

            Console.WriteLine(a);
            EmpType e1 = EmpType.Manager;

            AskForBonus(e1);
            Console.ReadLine();
        }
Пример #17
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Enums ******");

            //Создать тип подрядчика
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);
            Console.WriteLine("EmpType uses a {0} for storage", Enum.GetUnderlyingType(emp.GetType()));
            Console.WriteLine("EmpType uses a {0} for storage", Enum.GetUnderlyingType(typeof(EmpType)));
            Console.WriteLine("emp is a {0}.", emp.ToString());
            Console.WriteLine("{0} = 1", emp.ToString(), (int)emp);
            EmpType      e2  = EmpType.Contractor;
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);

            Console.ReadLine();
        }
Пример #18
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Enums *****");
            // Make an EmpType variable.
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);
            Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine("**** Fun with underlying Types of Enums*****");
            // Make a contractor type.
            EmpType emp2 = EmpType.Contractor;

            AskForBonus(emp2);
            // Print storage for the enum.
            Console.WriteLine("EmpType uses a {0} for storage", Enum.GetUnderlyingType(emp2.GetType()));
            Console.ReadLine();
            Console.WriteLine();

            // This time use typeof to extract a Type.
            Console.WriteLine("EmpType uses a {0} for storage", Enum.GetUnderlyingType(typeof(EmpType)));
            Console.WriteLine();

            Console.WriteLine("**** Fun with ToString Method *****");
            EmpType emp3 = EmpType.Contractor;

            AskForBonus(emp3);
            // Prints out "emp is a Contractor".
            Console.WriteLine("emp3 is a {0}.", emp3.ToString());
            Console.ReadLine();

            Console.WriteLine("**** Fun mit Enum Werte - Konsolenausgabe *****");
            EmpType emp4 = EmpType.Contractor;

            // Prints out "Contractor = 100".
            Console.WriteLine("{0} = {1}", emp4.ToString(), (byte)emp4);
            Console.ReadLine();
            Console.WriteLine();

            Console.WriteLine("**** Fun with Enums *****");
            EmpType e2 = EmpType.Contractor;
            // These types are enums in the System namespace.
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);
            Console.ReadLine();
        }
Пример #19
0
        public static void JustAMethod()
        {
            Console.WriteLine("Please Select your Designation");
            Console.WriteLine("1. Manager");
            Console.WriteLine("2. Sr. Manager");
            Console.WriteLine("3. Program Manager");
            Console.WriteLine("4. Director");
            Console.WriteLine("5. Senior Director");

            int choosedOne = Convert.ToInt32(Console.ReadLine());

            EmpType empType = (EmpType)choosedOne;

            string choosedDesignation = empType.ToString();
        }
Пример #20
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with enums ****");
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            Console.WriteLine("EmpType uses a {0} as storage", Enum.GetUnderlyingType(emp.GetType()));
            Console.WriteLine("emp name is {0}, and value: {1}", emp.ToString(), Enum.Format(emp.GetType(), emp, "D"));
            DayOfWeek    dw = DayOfWeek.Monday;
            ConsoleColor cc = ConsoleColor.Red;

            EvaluateEnum(emp);
            EvaluateEnum(dw);
            EvaluateEnum(cc);
            Console.ReadLine();
        }
Пример #21
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Enums *****\n");
            EmpType e2 = EmpType.Contractor;

            AskForBonus(e2);
            Console.WriteLine(typeof(EmpType));
            Console.WriteLine("emp is a {0}", e2.ToString());

            // These types are enums in the System namespace.
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);
            Console.ReadLine();
        }
Пример #22
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun With Enums *****");
            // Make an EmpType variable
            EmpType emp = EmpType.Contractor;

            Console.WriteLine("emp is an {0} which holds the value {1}.", emp.ToString(), (byte)emp);
            AskForBonus(emp);
            // print storage for the enum
            Console.WriteLine("EmpType uses a {0} for storage", Enum.GetUnderlyingType(typeof(EmpType)));
            DayOfWeek    day  = DayOfWeek.Monday;
            ConsoleColor cyan = ConsoleColor.Cyan;

            EvaluateEnum(emp);
            EvaluateEnum(day);
            EvaluateEnum(cyan);
            Console.ReadLine();
        }
Пример #23
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Enums *****\n");
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            // Print storage for the enum.
            //Console.WriteLine("EmpType uses a {0} for storage", Enum.GetUnderlyingType(emp.GetType()));
            Console.WriteLine("EmpType uses a {0} for storage", Enum.GetUnderlyingType(typeof(EmpType)));

            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);
            Console.WriteLine();

            EmpType      e2  = EmpType.Contractor;
            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);

            Console.ReadLine();
        }
Пример #24
0
        public static void TestEnums()
        {
            #region enum instances methods


            EmpType emp1 = EmpType.Manager;
            // ------------------------<  obj.ToString()  >------------------------
            Console.WriteLine(emp1);
            Console.WriteLine($"emp1.ToString() = {emp1.ToString()}");

            // ------------------------<  obj.GetType()  >------------------------
            Console.WriteLine($"emp1.GetType() = {emp1.GetType()}");    //pro_csharp_book_training.EmpType

            // ------------------------<  obj.GetTypeCode()  >------------------------
            Console.WriteLine($"emp1.GetTypeCode() = {emp1.GetTypeCode()}");    //Int32

            // ------------------------<  obj.CompareTo(object target)  >------------------------
            // NOTE: target argument should be in the same type of enumeration
            Console.WriteLine($"emp1.CompareTo(EmpType.Manager) = {emp1.CompareTo(EmpType.Manager)}");         // 0
            Console.WriteLine($"emp1.CompareTo(EmpType.SalesPersom) = {emp1.CompareTo(EmpType.SalesPersom)}"); // -1
            //Error:passing string not EmptType
            //Console.WriteLine($@"emp1.CompareTo(""Manager"") = {emp1.CompareTo("Manager")}");
            //Error:passing int not EmptType
            //Console.WriteLine($"emp1.CompareTo(0) = { emp1.CompareTo(0)}");

            // ------------------------<  obj.Equals(object target)  >------------------------
            // Equals : it accepts underline storage or enum constant name,
            //          so it doesn't throw Exception in Type Mismatch
            Console.WriteLine($"emp1.Equals(EmpType.Manager) = {emp1.Equals(EmpType.Manager)}");         // True
            Console.WriteLine($"emp1.Equals(EmpType.SalesPersom) = {emp1.Equals(EmpType.SalesPersom)}"); // False
            Console.WriteLine($"emp1.Equals(1) = {emp1.Equals(1)}");                                     // False
            Console.WriteLine($@"emp1.Equals(""Managers"") = {emp1.Equals("Manager")}");                 // False
            #endregion

            #region Get Type of enum
            // ------------------------<  System.Enum Class Functionality  >------------------------
            // -------------------------------------------------------------------------------------
            Console.WriteLine($"Enum.GetUnderlyingType(typeof(EmpType)) = {Enum.GetUnderlyingType(typeof(EmpType))} "); // Int32
            #endregion

            #region Array Enum.GetValues(Type enumtype);

            // ------------------------<   public static Array GetValues(Type enumType);  >------------------------
            Console.WriteLine("1- Array empItems = Enum.GetValues(typeof(EmpType))");
            Array empItems = Enum.GetValues(typeof(EmpType));
            foreach (EmpType item in empItems)
            {
                Console.WriteLine($"Name: {item} , Value: {(int)item}");
            }
            Console.WriteLine();


            // ------------------------<  public static Array GetValues(Type enumType);  >------------------------
            // The same as the last code but here we see some changes
            Console.WriteLine("2- Array empItems = Enum.GetValues(typeof(EmpType))");
            empItems = Enum.GetValues(typeof(EmpType));
            for (int i = 0; i < empItems.Length; i++)
            {
                Console.WriteLine("Name: {0} , Value: {0:D}", empItems.GetValue(i));
            }
            Console.WriteLine();

            // ------------------------<  public static Array GetValues(Type enumType);  >------------------------
            // The same as the last code but here we see some changes
            Console.WriteLine("3- Enum.GetValues(emp1.GetType())");
            foreach (EmpType item in Enum.GetValues(emp1.GetType()))
            {
                //Console.WriteLine($"Name: {item} , Value: {(int)item}");
                Console.WriteLine("Name: {0} , Value: {1}", item, (int)item);
            }
            Console.WriteLine();
            #endregion

            #region public static string[] GetNames(Type enumType)
            // ------------------------<  public static string[] GetNames(Type enumType);  >------------------------
            Console.WriteLine("Enum.GetNames()");
            foreach (string item in Enum.GetNames(typeof(EmpType)))
            {
                Console.WriteLine($"{item}");
            }
            Console.WriteLine();
            #endregion

            #region public static string GetName(Type enumType, object value);
            // ------------------------<  public static string GetName(Type enumType, object value);  >------------------------
            Console.WriteLine("Enum.GetName(typeof(EmpType),0) = {0}", Enum.GetName(typeof(EmpType), 0));
            #endregion

            #region public static object Parse(Type enumType, string value);
            // ------------------------<  public static object Parse(Type enumType, string value);  >------------------------
            Console.WriteLine("public static object Parse(Type enumType, string value);");
            emp1 = EmpType.SalesPersom;
            Console.WriteLine(emp1.ToString());
            emp1 = (EmpType)Enum.Parse(typeof(EmpType), "Manager");
            Console.WriteLine(@"Enum.Parse(typeof(EmpType),""Manager"") = " + emp1);

            emp1 = (EmpType)Enum.Parse(typeof(EmpType), "2");
            Console.WriteLine(@"Enum.Parse(typeof(EmpType),""2"") = " + emp1);
            #endregion

            #region public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct;
            // -----<  public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct;  >------

            if (Enum.TryParse("PTSalesPerson", out EmpType emp2))
            {
                Console.WriteLine($"TryParse string (PTSalesPerson) Result:");
                WriteEnum(emp2);
            }
            else
            {
                Console.WriteLine("Can't Parse string");
            }

            if (Enum.TryParse("2", out emp2))
            {
                Console.WriteLine($"TryParse integer 2 Result:");
                WriteEnum(emp2);
            }
            else
            {
                Console.WriteLine("Can't Parse integer");
            }
            #endregion

            #region public static object ToObject(Type enumType, int value);
            // -----<  public static object ToObject(Type enumType, int value);  >------
            Console.WriteLine(new StringBuilder().Append('=', 30));
            Console.WriteLine("public static object ToObject(Type enumType, int value);");
            // To convert integer any provided value type to enum
            EmpType emp3 = (EmpType)Enum.ToObject(typeof(EmpType), 2);
            WriteEnum(emp3);
            WriteEnumNameValuePair(emp3.GetType());
            #endregion

            #region public static bool IsDefined(Type enumType, object value);


            // -----<  public static bool IsDefined(Type enumType, object value);  >------
            Console.WriteLine();
            Console.WriteLine("public static bool IsDefined(Type enumType, object value);");
            Console.WriteLine($@"Enum.IsDefined(typeof(Day),""Sat"") = {Enum.IsDefined(typeof(Day),"Sat")}");
            Console.WriteLine($@"Enum.IsDefined(typeof(Day),""sat"") = {Enum.IsDefined(typeof(Day), "sat")}");
            Console.WriteLine($@"Enum.IsDefined(typeof(Day),""Moamen"") = {Enum.IsDefined(typeof(Day), "Moamen")}");
            Console.WriteLine();
            #endregion

            #region public static string Format(Type enumType, object value, string format);
            //-----<  public static string Format(Type enumType, object value, string format);  >------
            //  Format          Description
            //"G" or "g"        If value is equal to a named enumerated constant,
            //                  the name of that constant is returned; otherwise,
            //                  the decimal equivalent of value is returned.

            //                  For example, suppose the only enumerated constant is named Red,
            //                  and its value is 1.If value is specified as 1, this format returns "Red".However,
            //                  if value is specified as 2, this format returns "2".
            ///////////////// there are another part here in Microsoft APIs about FlagsAttribute.

            // "X" or "x"       Represents value in hexadecimal format without a leading "0x".

            // "D" or "d"       Represents value in decimal form.

            // "F" or "f"       Behaves identically to "G" or "g", except that
            //                  the FlagsAttribute is not required to be present on the Enum declaration.
            Console.WriteLine("------------------- General Format --------------------");
            Console.WriteLine($@"Enum.Format(Day, Day.Sat,""G"") = {Enum.Format(typeof(Day), Day.Sat, "G")}");
            Console.WriteLine($@"Enum.Format(Day, Day.Sun,""G"") = {Enum.Format(typeof(Day), Day.Sun, "G")}");
            Console.WriteLine($@"Enum.Format(Day, Day.Mon,""G"") = {Enum.Format(typeof(Day), Day.Mon, "G")}");

            Console.WriteLine("------------------- Decimal Format --------------------");
            Console.WriteLine($@"Enum.Format(Day, Day.Sat,""D"") = {Enum.Format(typeof(Day), Day.Sat, "D")}");
            Console.WriteLine($@"Enum.Format(Day, Day.Sun,""D"") = {Enum.Format(typeof(Day), Day.Sun, "D")}");
            Console.WriteLine($@"Enum.Format(Day, Day.Mon,""D"") = {Enum.Format(typeof(Day), Day.Mon, "D")}");

            Console.WriteLine("------------------- HexDecimal Format --------------------");
            Console.WriteLine($@"Enum.Format(Day, Day.Sat,""x"") = {Enum.Format(typeof(Day), Day.Sat, "x")}");
            Console.WriteLine($@"Enum.Format(Day, Day.Sun,""x"") = {Enum.Format(typeof(Day), Day.Sun, "x")}");
            Console.WriteLine($@"Enum.Format(Day, Day.Mon,""x"") = {Enum.Format(typeof(Day), Day.Mon, "x")}");
            #endregion

            // Test: public static void WriteEnumNameValuePair(Type enumType);
            WriteEnumNameValuePair(emp2.GetType());
            WriteEnumNameValuePair(typeof(Day));
        }
Пример #25
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Enums as parameters *****");
            EmpType fred;

            fred = EmpType.VP;
            AskForBonus(fred);

            // Print out string version of ‘fred’.
            Console.WriteLine("\n***** ToString() *****");
            Console.WriteLine(fred.ToString());

            //Get underlying type.
            Console.WriteLine("\n***** Enum.GetUnderlyingType() *****");
            Console.WriteLine(Enum.GetUnderlyingType(typeof(EmpType)));

            // Get Fred's type, hex and value.
            Console.WriteLine("\n***** Enum.Format() *****");
            Console.WriteLine("You are a {0}", fred.ToString());
            Console.WriteLine("Hex value is {0}", Enum.Format(typeof(EmpType), fred, "x"));
            Console.WriteLine("Int value is {0}", Enum.Format(typeof(EmpType), fred, "D"));

            // Parse.
            Console.WriteLine("\n***** Enum.Parse() *****");
            EmpType sally = (EmpType)Enum.Parse(typeof(EmpType), "Manager");

            Console.WriteLine("Sally is a {0}", sally.ToString());

            // Get all stats for EmpType.
            Console.WriteLine("\n***** Enum.GetValues() *****");
            Array obj = Enum.GetValues(typeof(EmpType));

            Console.WriteLine("This enum has {0} members:", obj.Length);

            // Now show the string name and associated value.
            foreach (EmpType e in obj)
            {
                Console.Write("String name: {0}", Enum.Format(typeof(EmpType), e, "G"));
                Console.Write(" ({0})", Enum.Format(typeof(EmpType), e, "D"));
                Console.Write(" hex: {0}\n", Enum.Format(typeof(EmpType), e, "X"));
            }

            // Does EmpType have a SalePerson value?
            Console.WriteLine("\n***** Enum.IsDefined() *****");
            if (Enum.IsDefined(typeof(EmpType), "SalesPerson"))
            {
                Console.WriteLine("Yep, we have sales people.");
            }
            else
            {
                Console.WriteLine("No, we have no profits....");
            }

            Console.WriteLine("\n***** < and > *****");
            EmpType Joe  = EmpType.VP;
            EmpType Fran = EmpType.Grunt;

            if (Joe < Fran)
            {
                Console.WriteLine("Joe's value is less than Fran's value.");
            }
            else
            {
                Console.WriteLine("Fran's value is less than Joe's value.");
            }

            Console.ReadLine();
        }
Пример #26
0
        static void Main(string[] args)
        {
            Console.WriteLine("=> Practicing Enum ");
            EmpType emp = EmpType.Contractor;

            AskForBonus(emp);

            Console.WriteLine("Emp is a {0}", emp.ToString());

            //Scovare il valore di un certo membro di enum
            Console.WriteLine("{0} = {1}", emp.ToString(), (byte)emp);

            DayOfWeek    day = DayOfWeek.Monday;
            ConsoleColor cc  = ConsoleColor.Gray;
            EmpType      e2  = EmpType.Contractor;

            EvaluateEnum(e2);
            EvaluateEnum(day);
            EvaluateEnum(cc);

            Console.WriteLine("=> Structure exersices");
            Point myPoint;

            myPoint.x = 349;
            myPoint.y = 78;
            myPoint.Display();

            myPoint.Increment();
            myPoint.Display();

            Console.WriteLine("Settin up a new default point, with x = 0, y= 0");
            Point p2 = new Point();

            p2.Display();

            Console.WriteLine();
            Console.WriteLine("=> setting a Rectangle Struct with a reference to a class named ShapeInfo");

            Rectangle r1 = new Rectangle("First Rectangle", 12, 43, 12, 43);

            Console.WriteLine("Assigning r1 to r2");

            Rectangle r2 = r1;

            Console.WriteLine("\nChanging r2 values");
            r2.RectInfo.InfoString = ("That's a brand new info");
            r2.RectBottom          = 18;

            Console.WriteLine("Printing both: \n");

            r1.Display();
            r2.Display();

            Console.WriteLine("**** Person Classes****\n");
            Person fred = new Person("Fred", 12);

            fred.Display();

            Console.WriteLine("\nPrima della modifica");
            SendAPersonByValue(fred);
            Console.WriteLine("Dopo la modifica");
            fred.Display();

            Console.WriteLine("\nUsando ref l'oggetto viene cambiato");
            Person mel = new Person("Mel", 32);

            Console.WriteLine("\nPrima della modifica");
            mel.Display();
            SendAPersonByValue(ref mel);
            mel.Display();
        }
Пример #27
0
 /// <summary>
 /// Podemos usar o ToString() para imprimir pares chave/valor de um enum
 /// </summary>
 /// <param name="emp"></param>
 private static void PrintParKeyValue(EmpType emp)
 {
     Console.WriteLine("emp is a {0}.", emp.ToString());       // nome variavel enum Contractor
     Console.WriteLine("{0} = {1}", emp.ToString(), (int)emp); // valor variavel enum Contractor = 2
     Console.WriteLine("\n");
 }