menu

Wednesday, August 28, 2013

Inheritance in C#

using System;
namespace Inheritance
{
    interface I1
    {
        void Fun();
    }
    public abstract class A
    {
        // Implemented function can also be marked as virtual
        public virtual void Fun()
        {
            Console.WriteLine("I1::Fun()");
        }
        public abstract void Fun1();
        public virtual void Fun2()
        {
            Console.WriteLine("C1::Fun()");
        }
    }
    public class C1 : A
    {
        // Overriding abstract function of class A
        public override void Fun1()
        {
            Console.WriteLine("C1::Fun1()");
        }
        // Overriding virtual function of class A
        public override void Fun2()
        {
            Console.WriteLine("C1::Fun2()");
        }
    }
    public class C2 : C1
    {
        // Overriding virtual function of class A
        public override void Fun()
        {
            Console.WriteLine("C2::Fun()");
        }
        // Overriding abstract function of class A
        public override void Fun1()
        {
            Console.WriteLine("C2::Fun1()");
        }
        // Overriding virtual function of class A
        public override void Fun2()
        {
            Console.WriteLine("C2::Fun2()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Console.ReadKey();
        }
    }
}


Implement Two Interfaces With Same Method Name in C#

using System;
interface I1
{
    void Fun();
}
interface I2
{
    void Fun();
    void Fun1();
}
public class C : I1, I2
{
    public void Fun()  
    {
        Console.WriteLine("I1::Fun()");
    }
    /* This function will be implemented as private
     * because one more implementation with same name
     * is already from another base type(I1)*/
    void I2.Fun()
    {
        Console.WriteLine("I2::Fun()");
    }
    public void Fun1()
    {
        Console.WriteLine("I2::Fun1()");
    }
}
public class A
{
    public static void Main()
    {
        I1 o1 = new C();
        o1.Fun();            // Of I1

        I2 o2 = new C();
        o2.Fun();            // Of I2
        o2.Fun1();           // Of I2
       
        C o = new C();
        o.Fun();             // Of I1
        o.Fun1();            // Of I1

        Console.ReadKey();
    }

}

Sunday, August 25, 2013

Generic in C#

Generic is a term which enables us to design classes or methods which differ as per types until it is declared and instantiated. For exp …

using System;
namespace Generic
{
    class Program
    {
        public static void Main()
        {
            B<int> obj = new B<int>();
            System.Console.WriteLine(obj.Compare(2, 6));

            B<string> obj1 = new B<string>();
            System.Console.WriteLine(obj1.Compare("KUNAL", "MAURYA"));

            B<char> obj2 = new B<char>();
            System.Console.WriteLine(obj2.Compare('X', 'X'));

            ComplexNumber o1 = new ComplexNumber(2, 3);
            ComplexNumber o2 = new ComplexNumber(4, 8);
            B<ComplexNumber> obj3 = new B<ComplexNumber>();
            System.Console.WriteLine(obj3.Compare(o1, o2));

            Console.ReadKey();
        }
    }
    public class B  // Generic Class for all types having Equals method
    {
        public string Compare(myType a, myType b)
        {
            if (a.Equals(b))
            {
                return "Equal";
            }
            else
            {
                return "Not-Equal";
            }
        }
    }
    public class ComplexNumber
    {
        int a;
        int b;
        public ComplexNumber(int x, int y)
        {
            a = x;
            b = y;
        }
        public bool Equals(ComplexNumber z)
        {
            if (this.a.Equals(z.a) && this.b.Equals(z.b))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}


JavaScript Object

Object is special type of data with properties and methods. Almost every thing in javascript is an object. 

<script type="text/javascript">
    var arr = new Array(10);
    for (var i = 0; i < 10; i++) {
        //arr[i] = { firstname: 'Kunal', age: 25 + i };
        //OR
        var kunal = new Object();
        kunal.firstname = 'Kunal Maurya';
        kunal.age = 20 + i;
        arr[i] = kunal;
    }
    for (var i = 0; i < 10; i++) {
        var kunal = arr[i];
        document.write(kunal.firstname + " is " + kunal.age + " years old.
"
);
        //OR
        //document.write("
" + arr[i].firstname + " is " + arr[i].age + " years old.");
    }

</script>

Difference Between Const, ReadOnly and Static in C#

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

namespace StaticReadOnlyConstant
{
    class Program
    {
        public static string str;                   //Ok
        //public static string str="Static";        //Ok
        static void Main(string[] args)
        {
            //----Static-------------------------------
            str = "Kunal Maurya";
            Console.WriteLine(str);

            //----ReadOnly-------------------------------
            //Console.WriteLine(ReadOnly.str);    //Ok cant access without object it is instance member
            ReadOnly ro = new ReadOnly();
            Console.WriteLine(ro.str);

            //----Constant-------------------------------
            Console.WriteLine(Constant.str);    //Ok
            Constant co = new Constant();
            //Console.WriteLine(co.str);          //Not Ok const variables are class members like static, it has one instance only
            Console.ReadKey();
        }
    }
    public class ReadOnly
    {
        //spublic readonly string str;     //Ok
        public readonly string str="ReadOnly";
        public ReadOnly()
        {
            str = "Kunal Maurya"//Ok readonly can be modified inside the constructor
        }
        public void Modify()
        {
            //str = "Kunal Maurya";  //Ok readonly can not be modified any where except declaration time and inside constructor
        }
    }
    public class Constant
    {
        //public const string str;     //Not Ok must be initialize at declaration time
        public const string str = "ReadOnly"; //Ok
        public Constant()
        {
            //str = "Kunal Maurya";  //Not Ok const can not be modified any where except declaration time
        }
    }
}