What are extension method?
It seems very simple question but it’s not. One time I have been asked this question by one of the interviewer. My answer was simply “Extension method will let us create new methods for a class without touching that class”.
With experience I got more matured and I learned many things. I realized following points will impact more during interviews,
- Extension methods are mainly useful when we are dealing with Third party libraries. We don’t have the source code of the class but with Extension methods we will be able to add new member functions into that class.
- It make us obey OCP – Open closed principle, which says “software entities should be closed for modifications but open for extensions. With extension methods we add new methods to existing classes without modifying it.
Above you read is an example for extension methods and for its .NET interview questions explanation read complete extension methods in C#. We also have a video for it with scenarios and where to use it: -
What are nullable types?
“Let us store null values inside value type variable.” Normally value type won’t allow us to store null values.
Example : we cannot say
intmyVar; myVar=null; // It will fail because value types won’t allow null values and “int” is value type.
string myVar2; myVar2=null; It will work because string is reverence type.
Now the question why we need value types?
Normally Nullabledatatypes introduced for dealing with issues in two datatypes.
One of the two possibilities are there with these variables
1) It may contain some values
2) It will contain a default value.
Confused? Every value type is associated with some default value for an instance for Boolean it is false and for datetime its Datetime.Min that is 1/1/0001
Let’s talk about a scenario,
- You have retrieved some value from database
- You convert that value into boolean and store in some variable of type boolean.
- Database may contain null values but your traditional value type cant.
- In case null values your variable will contain default values.
It may leads to incorrect operations. Ultimate solution will be using Nullable types which may contain one of the three values,
1) No Value
For technical trainings on various topics like WCF (Windows Communication Foundation), MVC (Model View Controller), Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com