Tuesday 8 May 2012

Classes in C#

Classes are data structures in C#. Its a blue print for an object.It has  Set of methods, properties are wrapped into a unit called Class.
or General Perception  making into a programmable unit called class.

for ex:
                 class Person
                  {
                         //Set of properties
                           String Name;
                           int Age;
                           String Color;
                           float weight;
                       //Set of methods
                      public string getPersonName(){return Name;}
                      public int getPersonAge(){return Age;}
                      public float getPersonWeight(){return weight;}
                      public string getPersonColor(){return Color;}


                      public string setPersonName(string Name){this.Name = Name;}
                      public int setPersonAge(int Age){this.Age=Age;}
                      public float setPersonWeight(int weight){this.weight=weight;}
                      public string setPersonColor(string Color){this.Color=Color;}
                  }


Indexers in C#--  Its a property in a class.class can be indexed like an array.
                 class indexDemo
                 {
                   int[] arr= new int[100];
                   public int this[int index]
                   {
                          get
                        {
                               return arr[index];
                         }
                     set { arr[index] = value; }
                    }
                }


indexDemocs1  cs1 = new  indexDemo ();
            cs1[0] = 1;
            cs1[1] = 2;
            for (int i = 0; i < 100; i++)
            {
                Console.Write(cs1[i]+"\t");
            }
disadvantage
       There is no way user can find upper bound, so index more than 100 will throw "IndexOutOfRangeException"


No comments:

Post a Comment