Friday, 15 December 2017

Enum GetValues and GetNames Methods

Create Shapes Enum

    public enum Shapes
    {
        Circle,
        Square,
        Rectangle,
        TryAngle
    }

Create integer array then call to Shapes Enum

            //Get Shapes value
            int[] shapeValues = (int[])Enum.GetValues(typeof(Shapes));

            Console.WriteLine("Values from Shapes Enum");
            foreach (int values in shapeValues)
            {
                Console.WriteLine(values);
            }

Create string array then call to Shapes Enum
            //Get Shapes names
            Console.WriteLine("Names from Shapes Enum");
            string[] shapeNames = (string[])Enum.GetNames(typeof(Shapes));

            foreach (string Names in shapeNames)
            {
                Console.WriteLine(Names);
            }
Print both Shapes and values
            //Get Shape names & Values
            Console.WriteLine("Names & values from ShapesValue Enum");
            foreach (ShapesValue shapes in Enum.GetValues(typeof(ShapesValue)))
            {
                Console.WriteLine("{0} - {1}", shapes, (int)shapes);
            }





No comments:

Post a Comment

Generic in c#

Generic used to code reuse, type safety and increase performance, it consists in declaring a Type with Type parameter that will be instant...