Friday, April 22, 2016

C# Cool Code: Convert Enum to List

namespace ConsoleApplication1
{
    class Program
    {
         enum SomeEnum
        {
            RED,
            BLUE,
            WHITE
           
        }
        class KeyValues
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }
        static void Main(string[] args)
        {

            List enums = ((IEnumerable)Enum.GetValues(typeof(SomeEnum)))
                .Select(c => new KeyValues() { Key = ((int)c).ToString(), Value = c.ToString() }).ToList();


            //List Model = new List(){ "1", "2", "3" };
            foreach (var item in enums.Select((value, i) => new { i, value }))
            {

                Console.WriteLine("Collection Object Values : " + item.value.Value);
                Console.WriteLine("Collection Index : "+ item.i);
            }
      }
}

C# Cool Code : Foreach with Index iterator values

namespace PlayWithCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            List Model = new List() { "1", "2", "3" };
            foreach (var item in Model.Select((value, i) => new { i, value }))
            {

                Console.WriteLine("Collection Object Values : " + item.value);
                Console.WriteLine("Collection Index : " + item.i);
            }
        }
    }