Wednesday, 3 September 2014

CLASS

1) What is Class ?
           A class is the blueprint from which individual objects.

2) What is constructor ?
A constructor in a class is a special type of subroutine called to create an object. It prepares the new object for use

 class Sample
{
             public string param1, param2;
             public Sample()     // Default Constructor
            {
              param1 = "Welcome";
              param2 = "Aspdotnet-Suresh";
            }
}
 
class Program
{
           static void Main(string[] args)
            {
                      Sample obj=new Sample();   // Once object of class created automatically constructor will be called
                    Console.WriteLine(obj.param1);
                    Console.WriteLine(obj.param2);
                    Console.ReadLine();
            }
}


3) Private & Public & Protected ?

Private :Variables and methods declared with private visibility are not accessible in the child class

Public :Variables and methods declared with public visibility are accessible; but public variables violate our goal of encapsulation

Protected : Variables and methods declared with protected visibility in a parent class are only accessible by a child class or any class derived from that class

4) What is Inheritance ?

Inheritance is when an object or class is based on another object or class, using the same implementation

public class ParentClass
{
           public ParentClass()
           {
           }

           public void print()
           {
        Console.WriteLine("I'm a Parent Class.");
         
} 

}

 public class ChildClass : ParentClass
{
    public ChildClass()
    {
        Console.WriteLine("Child Constructor.");
    }

    public
static void Main()
    {
        ChildClass child =
new ChildClass();
        child.print();
    }
}


5) What is sealed class ?
A sealed class is a class that cannot be inherited. Sealed classes are used to restrict the inheritance


    sealed class SealedClass
    {
        public int x;
        public int y;
    }


6)What is interface ?

An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition
 

Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference


interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation:  
    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }

    static void Main()
    {
        // Declare an interface instance.
        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

7)What is Abstact Class ?

They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented



//Abstract Class1
abstract class absClass1
{
    public abstract int AddTwoNumbers(int Num1, int Num2);
    public abstract int MultiplyTwoNumbers(int Num1, int Num2);
}

//Abstract Class2
abstract class absClass2:absClass1
{
    //Implementing AddTwoNumbers
    public override int AddTwoNumbers(int Num1, int Num2)
    {
        return Num1+Num2;
    }
}

//Derived class from absClass2
class absDerived:absClass2
{
    //Implementing MultiplyTwoNumbers
    public override int MultiplyTwoNumbers(int Num1, int Num2)
    {
        return Num1*Num2;
    } 
} 

8) Abstact Class Vs Interface



                     Interfaces

         Abstract Classes

 A class may inherit several interfaces.
A class may inherit only one abstract class.
Interfaces can only have consts and methods stubs
Abstract classes can have consts, members, method stubs and defined methods, whereas
All methods of an interface must be defined as public
Methods and members of an abstract class can be defined with any visibility
An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties


9) Output Parameter C#


   public void getValues(out int x, out int y )
      {
          Console.WriteLine("Enter the first value: ");
          x = Convert.ToInt32(Console.ReadLine());
          Console.WriteLine("Enter the second value: ");
          y = Convert.ToInt32(Console.ReadLine());
      }
   

        /* local variable definition */
         int a , b;
         
         /* calling a function to get the values */
         n.getValues(out a, out b);


10) Event & Delegates

Delegate and Event concepts are completely tied together. Delegates are just function pointers, That is, they hold references to functions.


11) Override

override stands for use one's authority to reject or cancel

abstract class ShapesClass
{
    abstract public int Area();
}
class Square : ShapesClass
{
    int side = 0;

    public Square(int n)
    {
        side = n;
    }
    public override int Area()
    {
        return side * side;
    } 
} 

12)Overloading


mechanism to have more than one method with same name but with different signature (parameters). A method can be overloaded on the basis of following properties
  1. Have different number of parameter
  2. Having same number of parameters but of different type
  3. Having same number and type of parameters but in different orders

public class test
{
    public void getStuff(int id)
    {}
    public void getStuff(string name)
    {}
}

13)What is virtual keyword ?
 
            The virtual keyword is used to modify a method, property, indexer, or event declaration   and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it.


      When a virtual method is invoked, the run-time type of the object is checked 
for an overriding member. The overriding member in the most derived class is 
called, which might be the original member, if no derived class has overridden 
the member.
 
        class A
        {
           public virtual void Test()
           {
         Console.WriteLine("A.Test");
           }
        }

         class B : A
         {
          public override void Test()
          {
     Console.WriteLine("B.Test");
          }
         }

14)

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...