A delegate is a class that can hold a reference to a method.it can hold references only to methods that match its signature.A delegate is a callback. Delegates allow for flexibility and fine-grain control in event handling. A delegate acts as an event dispatcher for the class that raises the event by maintaining a list of registered event handlers for the event. By convention,event delegates in the .NET Framework have two parameters, the source that raised the event and the data for the event.
Category: OOPS
When use interface and when abstract class
Since abstract and interface both provide abstract methods to be implemented by child class
Interface contains only declaration but not the implementation.
An abstract class can have implementation as well.
So if we have a scenario where we need a default implementation we will use abstract class else anything we can use.
But the best is if you use interface With abstract class.
Extension Methods in C#
With help of extension method you can add a new method to an existing type without recompile and modifying existing type.
e.g.
string s = “hello”; string i = s.UpperCasefirstLater();
Result : Hello
Here UpperCasefirstLater is a extension method. see below code
public static class MyExtensions
{
public static string UpperCasefirstLater(this String str)
{
if (str.Length > 0)
{
char[] array = str.ToCharArray();
array[0] = char.ToUpper(array[0]);
return new string(array);
}
}
}
Null-Conditional Operator in c# 6 and above
By using C#6 we can make better visibility and reduce code line.one of the feature to handle null is shown below …
Old way to handle null
public static string getEmpName(Employee emp)
{
String result;
if (emp != null)
{
result = emp.name;
}
return result;
}
New way to handle null
public static string getEmpName(Employee emp)
{
String result;
if (emp != null)
{
result = emp?.name;
}
return result;
}
If we want to return a default value in case of null we have to use ?? Operator as below
public static string getEmpName(Employee emp)
{
String result;
if (emp != null)
{
result = emp?.name ?? “NA”;
}
return result;
}
Difference in Interface and Abstract class in c#
Interface : An interface is like a contract which is followed by the class that implemented interface, it can have property,method,delegate or event. An interface just have declaration has no definition. Interface members are public by default,
Abstract Class : A class declared as abstract keyword.
It can not be instantiated because it is incompleted.
Abstract Class can only be used as a base class.
Any class inheriting an Abstract class must provide implementation for abstract members. Note– If child class do not want implementation for all abstract method mark child class as abstract.
Differences :
1) Abstract class can have definition and interface can’t have.
2)Abstract class can have access modifier and interface can’t have.
3)Abstract class can have fields and interface can’t have.
4) Abstract class can inherit an interface or another abstract class but interface can inherit in an interface.