What is solid principal in C#

S stands for SRP (Single responsibility principle)
A class should perform only task related to itsef not others.
e.g we have a employee class we have add,update,delete and fetchemployee method and on adding each employee we send a welcome mail.So we should not implement mail method in employee class.we should create a seperate class for mailing

O stands for OCP (Open closed principle)
A class should desighned in such a way if there is any change in a already develoved class should be closed for modification but open for extension.This is because if you change in a existing,well tested,fine running method, you will have to re-test all cases which was not needed

L stands for LSP (Liskov substitution principle)
This principal says parent should easily replace the child object. the derived classes should be perfectly substitutable for their base classes. If class D,E is derived from A then D or E should be substitutable for A.
for example We can achive LSP in a abstract method of abstract class by assighning child instance in parent object as below
A obj= new E():
A obj= new D():

I stands for ISP ( Interface segregation principle)
ISP says If we are inheriting any interface Clients should not be forced to implement interfaces they don’t use. Instead of one huge interface, many small interfaces are preferred based on groups of methods, each one serving one sub module.“

D stands for DIP ( Dependency inversion principle)
High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.”

e.g DI design pattern

What is delegate in C#

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.

IEnumerable and IQueryable Interface in C #

IEnumerable<T> Interface in C #

IEnumerable interface is a simple iteration over a collection of a specified type.It has Method GetEnumerator() that iterates through the collection.
Use: Mainly used to work with in memory data i.e List,Array
Example: IEnumerable<Product> products = db.employee.Take(3);

IQueryable Interface in C #

It is basically used to evaluate queries against a specific data source.
Use: Used to query database.e.g. iin LINQ
Example: IQueryable<Product> products = db.employee.Take(3);

Difference:

In IEnumerable All employees will be loaded After Loading all data program will filter
Query run on database- Select * from Employees
In IQueryable Data will be filterd on Database Program will get only top 3 recoreds
Query run on database- Select TOP 3 * from Employees

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.