Why use Abstract class

Whenever you want to define common functions and variables for the group of subclasses that share these common implementation but no need to create the object of super class.
Example:
Dog is an Animal, Tiger is an Animal, Bear is an Animal.
So that Animal can be an Abstract Class with common functionality like – No of legs, color etc
and no need to create an instance of Animal class rather than subclass

Advertisements

What is anonymous method

It is an inline unnamed method in the code.
It is created by the delegate keyword and doesn’t have return type.
or we can say, An anonymous method has no name, no optional parameters and no return type, it has only body.
Generally we use anonymous method in event handling.

Example:

class Program
{
//delegate for representing anonymous method
delegate int dele(int x, int y);
static void Main(string[] args)
{
//anonymous method using delegate keyword
dele ano = delegate(int x, int y) { return x * y; };
int result = ano(3, 3);
Console.WriteLine(result);
}
}

OUTPUT: 9

Disadvantages of LINQ

There are following disadvantages of LINQ

=> If we change any thing in our LINQ query then we have to recompile it and redeploy its dll to the server.
=> LINQ is not good for Write complex queries like SQL
=> LINQ doesn’t take the full advantage of SQL features like cached execution plan for stored procedure.
=> Performance will be degraded if LINQ query is not correct.

string and string builder

For a simple string concantenation code shown below it will create 3 copies of string in memory.

//The below line of code Creates one copy of the string
string str = “shiv”;

/* The below line of code creates three copies of string object one for the concatenation at right hand side and the other for new value at the left hand side. The first old allocated memory is sent for garbage collection.*/
str = str + “shiv”;

When you use the string builder for concatenation it will create only one copy of the object.

/* The below code uses string builder and only one
object is created as compared to normal string where we have 3 copies created*/
StringBuilder objBuilder = new StringBuilder();
objBuilder.Append(“shiv”);

Ok now summarizing what should we say to the interviewer.

String is immutable. Immutable means once assigned it can not be changed. Thats why it creates more copies of the object as it can not use the same instance.String builder is mutable , in other words the same object will changed rather than creating new objects.

Boxing and Unboxing

Converting a value type to reference type is called Boxing.
Unboxing is the opposite operation
Boxing confirm the conversion of value type variable or literals to the reference type

int i = 12;
object box = i;
if (box is int) {
Console.Write(“Box contains an int”);
}
————————————————–

struct Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Point p = new Point(10, 10);
object box = p;
p.x = 20;
Console.Write(((Point)box).x);

will output the value 10 on the console
—————————————-
Unboxing:

object box = 12;
int i = (int)box;
object box = new int_Box(12);
int i = ((int_Box)box).value;

Constructor and its Types

constructor is a class method that gets automatically executed whenever class’s object is created or whenever class is initialized

– Constructor does not have any return type
– Construtctor have the same name of the class

1.Instance or Default Constructor:——–
Constructor called implicitly when we create the class instance, the modifiers can be public, private, protected, and internal or protected internal.

2. static Constructor——— will call only once when the class gets loaded in the memory without creating the instance of the class.
– It can only access the static members of the class.
– There can be only one static constructor in the class.
– The static constructor should be without parameters.

3. Parameterized Constructors:———-
will require initializing class members during instantiation
– same rules as default constructor and will have parameters

4. A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.