menu

Wednesday, August 28, 2013

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
        }
    }
}


Thursday, August 16, 2012

Ref and Out


What is Ref & Out?
class Program
{
    public static void Calc(int a, int b, ref int x, ref int y, ref int z)
    {
        x = a + b;
        y = a - b;
        z = a * b;
    }
    static void Main(string[] args)
    {
        int p, q, sum, sub, mul;
        p = 5;
        q = 10;
        sum = 0;      //  in case of ref we have to initialize
        sub = 0;      //  variables whose reference is passing to function
        mul = 0;      //  before passing reference to function
        Calc(p, q, ref sum, ref sub, ref mul);
        Console.WriteLine("Sum: {0}", sum);
        Console.WriteLine("Sub: {0}", sub);
        Console.WriteLine("Mul: {0}", mul);
        Console.ReadKey();
    }
}

class Program
{
    public static void Calc(int a, int b, out int x, out int y, out int z)
    {
        x = a + b;  //  we have to initialize in the
        y = a - b;  //  function
        z = a * b;  // 
    }
    static void Main(string[] args)
    {
        int p, q, sum, sub, mul;
        p = 5;
        q = 2;
        //sum = 0;      //  in case of out we don't need to initialize
        //sub = 0;      //  variables whose reference is passing to function but
        //mul = 0;      //  the calling function have to intialize that reference

        Calc(p, q, out sum, out sub, out mul);
        Console.WriteLine("Sum: {0}", sum);
        Console.WriteLine("Sub: {0}", sub);
        Console.WriteLine("Mul: {0}", mul);
        Console.ReadKey();
    }
}

Stack and Heap

What is Stack and Heap?

Stack is a memory location where data type variables, reference type memory location and value type data are stored.
Ex. data type variables -> i,y,cls1 etc
reference type memory location -> ref
value type data -> 4,2

 Heap is a memory location where object of reference type data types are stored.
Ex. Object of class class1, see the figure.
CLR always looks for data in stack first, if reference type data type found then jumps to heap.