Tuesday, August 26, 2014

Understanding Static Constructor (C# reference)

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

namespace Apps
{

    class statiConstructor
    {
        private static int a;
        static statiConstructor()
        {
            a = 10;
            Console.WriteLine("Static Constructor with static member value=" + a);
        }
        public static void MethodCall()
        {
            Console.WriteLine("Method Call ");
        }
    }
    class UnderstandingStaticConstructor
    {
        public static void Main()
        {
         // In particular, a static constructor is called once, automatically, when the class is used for the first time.
            statiConstructor k = new statiConstructor();

            statiConstructor.MethodCall();
            statiConstructor.MethodCall();

        }
    }
}



Output:

Static Constructor with static member value = 10
Method Call
Method Call



Understanding Indexer in C#

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

namespace Apps
{
    class IndexerExample
    {
        //private int[] item = new int[11];

        private string[] nameslist = new string[11];
        private int index = 0; 
        public string this[int index]
        {
            get
            {
                return nameslist[index];
            }
            set
            {
                nameslist[index] = value;
            }
        }

        public int this[string indexContent]
        {
            get
            {
                for (int i = 0; i < nameslist.Length; i++)
                {
                    if (indexContent == nameslist[i])
                    {
                        index = i;
                        break;
                    }
                }
                return index;
            }
            set
            {
                for (int i = 0; i < nameslist.Length; i++)
                {
                    if (indexContent == nameslist[i])
                    {
                        nameslist[i] = value.ToString();
                    }
                }
            }
        }
    }
    class UnderstandingIndexer
    {
        public static void Main()
        {
            IndexerExample obj = new IndexerExample();
            obj[4] = "Nagendra";
            // calls the property with return type string
            Console.WriteLine(obj[4]);

            // calls the property with return type int
            Console.WriteLine(obj["Nagendra"]);
        }
    }
}

Indexer an object to be indexed in the same way as an array. Indexer modifier can be private, public, protected or internal. The return type can be any valid C# types.Indexers in C# must have at least one parameter.

Monday, August 18, 2014

Understanding interfaces in C#

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

namespace Apps
{
    // Interface is a collection of abstract members and properties.
    // Unlike the Abstract class the interface cannot contain the non-abstract members.

    interface Iclass1
    {
        // A property in interface can be specified without any body
        int a { set; get; }
        void PrintProperty();

        // Access modifiers for the members of interface defaults to Public
        void Read();
}

    interface Iclass2
    {
        void Write();
}

    class Test9 : Iclass1Iclass2      // Multiple interfaces
{

       // The class should implement all the methods and properties contained in the interface.(if not gives compile error)
        public int a { setget; }

        public void Print()
        {
            Console.WriteLine("Non-interface method implemented in class, Value=" + a);
        }

        public void PrintProperty()
        {
            Console.WriteLine(a);
        }

        public void Read()
        {
            Console.WriteLine("Reading example");
        }

        public void Write()
        {
            Console.WriteLine("Writing example");
        }
    }

    class UnderstandingInterfaces
    {
        public static void Main()
        {
            Iclass1 ts = new Test9();
            Iclass2 ts1 = new Test9();
            ts.Read();
            ts1.Write();
            ts.PrintProperty();

            Test9 ts2 = new Test9();
            ts2.Print();
        }
    }
}

Sunday, August 17, 2014

Understanding Runtime Polymorphism

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

namespace ConsoleApps
{
    class BaseClass
    {
        public void Normal()
        {
            Console.WriteLine("Base Class print");
        }
        public virtual void RuntimePoly()
        {
            Console.WriteLine("Base Class print");
        }
    }

    class DerivedClass : BaseClass
    {
        public new void Normal()
        {
            Console.WriteLine("Derived Class print");
        }

        public override void RuntimePoly()
        {
            Console.WriteLine("Derived Class print");
        }
    }

class UnderstandingRuntimePolymorphism
{
 public static void Main()
{
           // Here Base class method is called
           BaseClass nm = new DerivedClass();
           nm.Normal();

          // Here Derived class method is called
            // Due to run time decision (late binding or runtime polymorphism)
            // since the methods are overriden with ovveride keyword in derived class
            // and virtual keyword in base class of the methods

           BaseClass poly = new DerivedClass();
           poly.RuntimePoly();
     }
 }
}


Understanding Is and As operator

This example states the purpose of the "is" and "as" operator during casting.  
Both is and as operators are used to check the type of object referred by object reference and also type caste if possible. 

Operator is can be used to test whether an object reference refers to an object of the given type. 

Operator as can be used to type cast one type to another provided the expression is compatible with the given type. If not compatible than null is assigned to the object reference. 

like in example below 
c2 = rd2 as ResidentalDoctors1;  here c2 is assigned null because rd2 is consultant reference type and c2 is referring to resident type.



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

namespace Apps
{

    class Doctor1
    {
        protected string name, specialization;
        public Doctor1(string name, string specialization)
        {
            this.name = name;
            this.specialization = specialization;
        }
        public void Print()
        {
            Console.WriteLine(name);
            Console.WriteLine(specialization);
        }
    }
    class ResidentalDoctors1 : Doctor1
    {
        protected int salary;
        public ResidentalDoctors1(string name, string specialization, int salary)
            : base(name, specialization)
        {
            this.salary = salary;
        }
        public new void Print()
        {
            base.Print();
            Console.WriteLine(salary);
        }
        public int GetPay()
        {
            return salary;
        }
    }
    class Consultant1 : Doctor1
    {
        protected int fees, visitNum;
        public Consultant1(string name, string specialization, int fees, int visitNum)
            : base(name, specialization)
        {
            this.fees = fees;
            this.visitNum = visitNum;
        }
        public new void Print()
        {
            base.Print();
            Console.WriteLine(fees);
            Console.WriteLine(visitNum);
        }
        public int GetPay()
        {
            return fees * visitNum;
        }
    }



 class UnderstandingISandASoperator
 {
 public static void Main()
{
 //The below three lines of code will lead to exception i.e known at runtime. Since d reference is pointing to consultant, but it is assigned to c (ResidentalDoctors1 object)
Doctor1 d = new Consultant1("Chitti", "General", 300, 10);  // Upcasting
ResidentalDoctors1 c;
c = (ResidentalDoctors1)d;  // Downcasting leads to exception. Not safe way to do this casting.

//  To overcome above problem we will see 2 ways to resolve this
 // 1. Example

Doctor1 rd = new Consultant1("Steve", "General", 100, 100);
ResidentalDoctors1 c;

 if (rd is ResidentalDoctors1)       // using is operator we are handling to avoid exception.
{
 // Below statements are executed only if rd is object reference to ResidentalDoctors1
c = (ResidentalDoctors1)rd;  // Downcasting leads to exception
Console.WriteLine("rd is a object reference to ResidentalDoctors1 class");
}

//  2. Example

Doctor1 rd2 = new Consultant1("Steve", "General", 100, 100);
ResidentalDoctors1 c2;

c2 = rd2 as ResidentalDoctors1; 
 // Here there is no condition and to check unlike is operator but as operator will handle exception internally and assigns null to it if thrown.
Console.WriteLine(c2);


 // This example below will complete the casting as c3 is referring to Consultant1 and rd3 is downcasted to Consultant1.
Doctor1 cd3 = new Consultant1("Steve", "General", 100, 100);
Consultant1 c3;
c3 = cd3 as Consultant1;
Console.WriteLine(c3);

        }
    }

}