menu

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.

Command Line Argument in C Sharp

What is command line argument?
class Program
{
    static void Main(string[] args)
    {
        foreach (string s in args)
        {
            Console.WriteLine(s);
        }
        Console.ReadKey();
    }
}

Jagged Array


What is jagged array?
    class Program
    {
        static void Main(string[] args)
        {
            int[][] arr = new int[3][]; // Jagged array
            int[] x1 = { 1,2,3,4,5,6,7,8,9,0};
            int[] x2 = { 1,3,5,7,9};
            int[] x3 = { 2,4,6,8};

            arr[0] = x1;
            arr[1] = x2;
            arr[2] = x3;

            for (int j = 0; j < 3; j++)
            {
                for (int i = 0; i < arr[j].Length; i++)
                {
                    Console.Write(arr[j][i].ToString()+ ", ");
                }
                Console.WriteLine();
            }
        }
    }

Constructor in C Sharp


Constructor: Is like a function without return type, its name must be same as class name.
Q1.         How many type of constructor are there in C#?
1.       System defined default constructor - > when user does not define constructor then compile implicitly provides default constructor.
2.       User defined constructor -> when user explicitly provides constructor. After that implicit default constructor will not be loaded, if needed then have to define explicitly.
3.       User Defined Parameterized constructor -> constructor with argument ex. SqlConnection(string conStr)
4.       Static, public, private, protected, internal etc.
Q2.         What is constructor chaining?
When we call constructor with new keyword and try to create object of class then it first it go to super class constructor to initialise super class first and then it come to initialise calling constructor’s class. See example of Q4.
Q3.         How to pass data to base class?
Through BASE keyword, see example of Q4
Q4.         What is overloaded constructor?
More than one constructor in a class is known as overloaded constructor. See example of Q4
Q5.         What is static Constructor?
Constructor with static keyword,
It will be called only once during program’s lifecycle, when first object of class will be created
It cannot have access modifier and
Only one static constructor a class can have.
Q6.         What will be the O/P of programs?
public class A
{
    public A()
    {
        Console.WriteLine("A");
    }
}
public class B:A
{
    public B()
    {
        Console.WriteLine("B");
    }
}
class Program
{
    static void Main(string[] args)
    {
        B objB = new B();
        Console.WriteLine("C");
        Console.ReadKey();
    }
}

public class A
    {
        static A() // this static constructor will called only once
        {
            Console.WriteLine("A");
        }
        public A(int x)
        {
            Console.WriteLine("A1");
        }
    }
    public class B : A
    {
        public B():base(1)
        {
            Console.WriteLine("B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            Console.WriteLine("C");
            Console.ReadKey();
        }
    }

    public class B
    {
        private B()
        {
            Console.WriteLine("B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            Console.WriteLine("C");
            Console.ReadKey();
        }
    }

    public class B
    {
        public static B()
        {
            Console.WriteLine("B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            Console.WriteLine("C");
            Console.ReadKey();
        }
    }