C#

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 10 November 2013

Enums How it works

----------------------------Program 1--------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {

        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }


    }


    class Program
    {
        static void Main(string[] args)
        {

            Employee[] e1 = new Employee[3];

            e1[0] = new Employee
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            };

            e1[1] = new Employee
            {
                Name = "Abhishek",
                Age = 21,
                Sex = 1
            };

            e1[2] = new Employee
            {
                Name = "Sita",
                Age = 22,
                Sex = 2
            };

            foreach (Employee emp in e1)
            {

                Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));
         

            }
            Console.ReadLine();
        }//main


        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }//class that has main
}//namespace
   
----------------------------Program 2--------------------------------------------
Employee[] e1 = new Employee[2];

If you add only one employee
Error
----------------------------Program 3--------------------------------------------
So better solution is to use list

List<Employee> ls = new List<Employee>();

            ls.Add(new Employee()
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            });

            ls.Add(new Employee()
            {
                Name = "Abhi",
                Age = 21,
                Sex = 1
            });
----------------------------Program 4--------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {

        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }


    }


    class Program
    {
        static void Main(string[] args)
        {

            List<Employee> ls = new List<Employee>();

            ls.Add(new Employee()
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            });

            ls.Add(new Employee()
            {
                Name = "Abhi",
                Age = 21,
                Sex = 1
            });

         

            foreach (Employee emp in ls)
            {

                Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));
         

            }
            Console.ReadLine();
        }


        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }
}
----------------------------Program 5--------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {

        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }


    }


    class Program
    {
        static void Main(string[] args)
        {

            Employee[] e1 = new Employee[3]
           
            {
           
             new Employee
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            },
            new Employee
            {
                Name = "Abhishek",
                Age = 21,
                Sex = 1
            },

            new Employee
            {
                Name = "Sita",
                Age = 22,
                Sex = 2
            }

            };







            foreach (Employee emp in e1)
            {

                Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));
         

            }
            Console.ReadLine();
        }


        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }
}
----------------------------Program 6--------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {

        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }


    }


    class Program
    {
        static void Main(string[] args)
        {

            //this is same as like
            //int[] marks =  new int[5] {1,2,3,4,5}

            Employee[] e1 = new Employee[3]
           
            {
           
             new Employee
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            },
            new Employee
            {
                Name = "Abhishek",
                Age = 21,
                Sex = 1
            },

            new Employee
            {
                Name = "Sita",
                Age = 22,
                Sex = 2
            }

            };







            foreach (Employee emp in e1)
            {

                Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));
         

            }
            Console.ReadLine();
        }


        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }
}
----------------------------Program 7--------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {

        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }


    }


    class Program
    {
        static void Main(string[] args)
        {

            //this is same as like
            //int[] marks =  new int[5] {1,2,3,4,5}

            Employee[] e1 = new Employee[3]
           
            {
           
             new Employee
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            },
            new Employee
            {
                Name = "Abhishek",
                Age = 21,
                Sex = 1
            },

            new Employee
            {
                Name = "Sita",
                Age = 22,
                Sex = 2
            }

            };



            Employee[] e2 = e1;



            foreach (Employee emp in e2)
            {

                Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));
         

            }
            Console.ReadLine();
        }


        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }
}

----------------------------Program 8--------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //this is same as like
            //int[] marks =  new int[5] {1,2,3,4,5}

            List<Employee> ls = null;

            Employee[] e1 = new Employee[3]      
            {
           
             new Employee
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            },
            new Employee
            {
                Name = "Abhishek",
                Age = 21,
                Sex = 1
            },

            new Employee
            {
                Name = "Sita",
                Age = 22,
                Sex = 2
            }

            };
            Employee[] e2 = e1;

            ls = new List<Employee>(e2);
            foreach (Employee emp in ls)
            {

                Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));
         

            }
            Console.ReadLine();
        }
        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }
}
----------------------------Program 9--------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //this is same as like
            //int[] marks =  new int[5] {1,2,3,4,5}

         

              List<Employee> ls = new List<Employee>()
            {
           
             new Employee
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            },
            new Employee
            {
                Name = "Abhishek",
                Age = 21,
                Sex = 1
            },

            new Employee
            {
                Name = "Sita",
                Age = 22,
                Sex = 2
            }

            };
          

            //ls = new List<Employee>(e2);
            foreach (Employee emp in ls)
            {

                Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));
         

            }
            Console.ReadLine();
        }
        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }
}
----------------------------Program 10-------------------------------------------
To count the length of list


  int length = ls.Count;
----------------------------Program 11-------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public class Employee
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //this is same as like
            //int[] marks =  new int[5] {1,2,3,4,5}

          Employee[] e1= new Employee[3];

              List<Employee> ls = new List<Employee>()
            {
           
             new Employee
            {
                Name = "Anurag",
                Age = 21,
                Sex = 1
            },
            new Employee
            {
                Name = "Abhishek",
                Age = 21,
                Sex = 1
            },

            new Employee
            {
                Name = "Sita",
                Age = 22,
                Sex = 2
            }

            };

//to convert list to array

              Employee[] e2 = ls.ToArray();


            //ls = new List<Employee>(e2);
            foreach (Employee emp in e2)
            {

            Console.WriteLine("Employee Detail is : {0},{1},{2}", emp.Name, emp.Age, Gender(emp.Sex));


            }
            Console.ReadLine();
        }
        public static string Gender(int gender)
        {
            switch (gender)
            {
                case 1:
                    return "Male";
                case 2:
                    return "Female";
                case 0:
                    return "Unknown";
                default:
                    return "invalid data";

            }
        }
    }
}
----------------------------Program 12-------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{

    public enum RGB
    {
   
        red,
        blue,
        green
   
    }

 
    class Program
    {
        static void Main(string[] args)
        {

            int[] values = (int[])Enum.GetValues(typeof(RGB));

            foreach (int a in values)
            {

                Console.WriteLine(a);//0 1 2

            }

            string[] Names = Enum.GetNames(typeof(RGB));

            foreach (string str in Names)
            {

                Console.WriteLine(str);// red blue green
           
            }

            Console.ReadLine();
        }
     
    }
}
----------------------------Program 13------------------------------------------
----------------------------Program 16-------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ENUM
{
    enum rgbvalues : int
    {

        blue = 598,
        green,
        red


    }



    class Program
    {
        static void Main(string[] args)
        {
            rgbvalues rgb1 = rgbvalues.blue;
            rgbvalues rgb2 = rgbvalues.green;
            rgbvalues rgb3 = rgbvalues.red;
            Console.WriteLine(rgb1);//blue[you cannot make same variable for the three...]
            Console.WriteLine(rgb2);//green
            Console.WriteLine(rgb3);//red

            int a = (int)rgb1;
            int b = (int)rgb2;
            int c = (int)rgb3;

            Console.WriteLine(a);//598
            Console.WriteLine(b);//599
            Console.WriteLine(c);//600
            Console.ReadLine();

        }
    }
}

//this is very simple......look at this you will get to know......
//if i initialise the 1st two and leave the last

//then the last follows the previous one.............