Polymorphism: Method hiding and overriding

Creating a method in derived class with same signature as a method in base class is called as method overriding.
Same signature means methods must have same name, same number of arguments and same type of arguments.
Method overriding is possible only in derived classes, but not within the same class.
When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract.

Method Hiding

If a method is not overriding the derived method, it is hiding it in the Derived class.  A hiding method has to be declared using the new keyword. Continue reading

What is the use of ENUM Flags

“Flags” is an ENUM attribute. If you want to set multiple values to an ENUM we need to use Flags.



enum MyColors


Green = 0,

Red = 1,

Blue = 2



In the below code we are setting “MyColors” ENUM to “Blue” and “Green” value.


MyColors color = MyColors.Blue | MyColors.Green;

if ((color & MyColors.Blue) == MyColors.Blue)


      Console.WriteLine(” Blue”);


if ((color & MyColors.Green) == MyColors.Green)


      Console.WriteLine(” Green”);


difference between variable and property

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.

An important difference is that interfaces can have properties but not fields.
Exposing a public field is an FxCop violation Continue reading

Dependency Inversion Principle, IoC Container, and Dependency Injection