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.
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
It can be possible by ISP (Interface segregation principle). Continue reading
“Flags” is an ENUM attribute. If you want to set multiple values to an ENUM we need to use Flags.
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)
if ((color & MyColors.Green) == MyColors.Green)
ENUM helps to define, manage and assign constants in effective way. Now the below sample code is good but the level values are not readable.
We can use “GetValues()” method of the “ENUM” class which returns an array of enum values.
var x = Enum.GetValues(typeof(MyColors));
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