Wednesday, 8 August 2012

C# questions


Q1. Can u call a method which is exist in two interfaces with same name and inherit both interfaces by the class?
Ans: No, call would be ambiguous. It will be difficult to identify that which method is called for the object.
You can understand by this code-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    interface pankaj11
    {
        abstract int method();
    }
    interface pankaj12
    {
        abstract int method();
    }
    class interfacechecking:pankaj11,pankaj12
    {
        abstract int method()
        {
            Console.WriteLine(90);
        }
        abstract int method()
        {
            Console.WriteLine(700);
        }
        static void Main(string[] args)
        {
            interfacechecking ob = new interfacechecking();
            ob.method();
            Console.WriteLine();
        }
    }
}


2. Difference between Structure and Class
1.  Structure is value type and class is reference type
2.  Structure does not support inheritance but a class supports.
3.  To make a structure we use Struct key word while to make a class we use Class keyword
4.  Structs are always derived from System.ValueType. They can also derive from any number of interfaces.
5.  Classes are always derived from one other class of your choosing. They can also derive from any number of interfaces.
6.  Class members are stored in heap but structure members are stored in Stack.


3. Difference between Value Type and Reference type
Data in C# is stored in a variable in one of two ways, depending on the type of the variable. This type will fall into one of two categories: reference or value. The difference is as follows:
1.   Value types store themselves and their content in one place in memory.
2.  Reference types hold a reference to somewhere else in memory (called the heap) where content is stored.
3.  One key difference between value types and reference types is that value types always contain a value, whereas reference types can be null.

4. Constructor:

Constructor is a special kind of a method which has the same name as the class name and no parameter (making it the default constructor for the class). A private default constructor, meaning that object instances of this class cannot be created using this constructor.

class MyClass
{
public MyClass()
{
// Default constructor code.
}
public MyClass(int myInt)
{
// Nondefault constructor code (uses myInt).
}
}
4.  What is abstract class?
Abstract class is class that is never instantiated and must be inherited by other class. It makes with the help of using abstract keyword.
Example:
abstract class PanakjAbs
{
int sum(int a, int b);
}
Class pankaj2:PanakjAbs
{
int sum(int s, int h)
{
return a+b;
}
Static void main(strings []args)
{
Panakj2 ob=new Panakj2();
int result=ob.sum(23,33);
Console.writeLine(result);
Console.Readkey();
}
}

5.   

No comments:

Post a Comment