Static Readonly variables

Why Static variables?

static variable shared the value of it among all instances of the class. Changes done to static variable are visible across methods. Static variable, that means it exists exactly one place in memory for the duration of the program. Static class variables are like globals. Static variables belong to type, not to its instance. You can change the value of a static variable in any of the method of that class.   Continue reading

Setting Return statement to Null in the finally block.

For example, If x is a local variable, will be effectively set to null anyway when the method is exited and the value of the return value is not null.

The finally statement is executed, but the return value isn’t affected. The execution order is:

  1. Code before return statement is executed
  2. Expression in return statement is evaluated
  3. finally block is executed
  4. Result evaluated in step 2 is returned

Continue reading

Difference between an Abstract class and an Interface.

There are technical differences between Abstract Classes and Interfaces, that being an Abstract Class can contain implementation of methods, fields, constructors, etc, while an Interface only contains method and property prototypes.

Two quick thoughts on differences between interfaces and abstract classes:

  1. Abstract classes desired if future expansion is likely, as an abstract class can be expanded, but an interface would have to be enhanced by addition of another interface, I2.

2. A class can inherit multiple interfaces but only one abstract class.

An Interface defines what something can do (how it behaves), and an Abstract Class defines what something is. Continue reading

Abstract Class Vs Interface

An abstract class is a special kind of class that has no implementation. It cannot be instantiated. Its implementation logic is provided by the classes that derive from it. It can have both abstract as well as non-abstract methods.
It is not compulsory to have only abstract methods in an abstract class. We can also have an abstract class with only non-abstract methods.

Why do we need an Abstract Class? Continue reading

Getting Top N rows from DataTable.

public DataTable getTop(DataTable dt, int n)
{
    DataTable outputTable = dt.Clone();
    for (int i = 0; i < n; i++)
        outputTable.ImportRow(dt.Rows[i]);
    return outputTable;
}

 

When to Call the Constructor of a Base Class.

You can use the base keyword in a constructor, to call the constructor of a class’ base class.  You’d most typically do this to allow the constructor in the base class to initialize the data members that are defined in the base class.  Then the constructor of the derived class can initialize its own data members.
Read more: http://csharp.2000things.com/2013/01/29/768-when-to-call-the-constructor-of-a-base-class/