Asp.net web API

ASP.Net Web API is a powerful platform/framework for creating HTTP enabled service APIs (web services) that exposes data services to more clients including mobile devices, tablets, browsers and traditional desktop applications.

ASP.Net Web API uses all the features of HTTP like URIs, request/response headers, various content formats caching, versioning and there is no need to define any extra configuration settings for different devices like WCF rest services.

ASP.Net Web API communicate with HTTP/HTTPs

The basic CRUD operations are mapped to the HTTP protocols in the following manner:
GET: This will be used to retrieve the required data from the remote resource.
POST: This will create a new entry for the current data that is being sent to the server.
PUT: This will update the current representation of the data on the remote server.
DELETE: This will delete the specified data from the remote server.

Features of ASP.NET WEB API
-It can be hosted with in the applicaion or on IIS.
-It has automatic support for OData.
-It supports convention-based CRUD Actions

Basics of HTTP protocol

What is HTTP protocol?
HTTP stand for Hypertext transfer protocol.
It is application level protocol
It is client server protocol

Request/Response

Requests are sent by one entity like web browser and response is sent by another entity like web server.
Between this request and response there are numerous entities, collectively designated as proxies, which perform different operations and act as gateways or caches.
In reality, there are more computers between a browser and the server handling the request: there are routers, modems, and more.

How does HTTP protocol works?

HTTP uses the TCP transport protocol to make this connection. By default, web traffic uses TCP port 80

Basics of HTTP protocol:
It is simple – Human readable
It is extensible
It is stateless but not sessionless
It mostly uses TCP transport protocol

Difference between ref and out parameters




Difference between ref and out parameters

Ref:
The ref keyword is used for passing parameter by reference
Need to be initialized before passing

Out:
The out paramter is used when method want to return multiple values
It is not mandatory to initialize paramter before passing

Ref and out in method overloading

Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time. Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.




class MyClass
{
public void Test(out int a) // compiler error “cannot define overloaded”
{
// method that differ only on ref and out”
}
public void Test(ref int a)
{
// method that differ only on ref and out”
}
}

However, method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument. The following example is perfectly valid to be overloaded.

class MyClass
{
public void Test(int a)
{

}
public void Test(out int a)
{
// method differ in signature.
}
}




Design Patterns

Design patterns:
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development.

Gang of Four (GOF)
Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 by the so-called “Gang of Four” (Gamma et al.), which is frequently abbreviated as “GoF”.

As per them..

  • Program to an interface not an implementation
  • Favor object composition over inheritance

Types of Design Patterns

  1. Creational Patterns:
    These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.
  2. Structural Patterns:
    These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
  3. Behavioral Patterns:
    These design patterns are specifically concerned with communication between objects.

Creational Patterns
Singleton
Factory method
Abstract factory
Builder
Prototype
Object Pool
Lazy initialization
Multiton
Prototype

Structural patterns
Adapter
Facade
Bridge
Composite
Decorator
FlyWeight
Proxy
Marker
Module
Twin

Behavioral patterns
Strategy
State
Observer or Publish/Subscribe
Chain of responsibility
Command
Interpreter
Iterator
Mediator
Null Object
TemplateMethod
Visitor
Blackboard
Memento
Servant
Specification

Details to be continued…

Citibank interview questions for experienced – dotnet

This questions were asked in Citibank Pune 2016.

  1. Tell me about yourself?
  2. Why to use abstract classes?
  3. What are interfaces?
  4. What is polymorphism?
  5. ref vs out
  6. Why c# supports single inheritance
  7. Why to use Var
  8. Design patterns used in project
  9. How to create singleton class?
  10. Is it thread safe?
  11. Why to use object for lock? Can we use integer class?
  12. lock vs Monitor vs Mutex?
  13. Explain MVVM pattern
  14. Prism Framework
  15. How to get second higest salary of an employee
  16. Real time example of Mutex?

Varian Medical System Interview Questions for experienced – Dotnet

1.  What is difference between Abstract class and Interfaces
2. What is virtual and override
3. What is Dependency injection? Use in your project
4. What is prism? Any other framework that you used like prism.
5. What is Extensible framework?
6. How to load assembly without using reflection
7. Have you created any custom controls? How to create them?
8. What are Dependency Properties?
9. How the hierarchy/Precedence is picked.. Say Style has background color = Red and control background color is set to white.
10. What are content template?
11. How to add edit button inside List Box?

Cognizant Interview Questions for experienced – Dotnet

Cognizant follows systematic interview process. They calls candidate between required experience range for dotnet, java and other technologies.
Process:

  1. Once you reach there office. They collect your resume along with Photo.
  2. You need to mention technology.
  3. Calls candidate based on in time.
  4. 1-2 rounds of technical interview.

Interview questions for dotnet candidate:

They divided the interview into 4 categories:

  1. OOPS
  2. .NetFramework and C#
  3. WPF
  4. DataBase and Agile

OOPs
Difference between Abtraction vs Encapsulation
What is Polymorphism
Interface vs Abstract.When to use what
Same method in interface, how to implement and call it.
Use of static constructor?
virtual and override keyword

C#
what is use of new keyword
when to use yield
Delegates VS Events
what are Generics
Extensionmethod – with example
Finalize vs Dispose- which handles managed/unmanaged code.

WPF
What are Dependency Properties?
Attached properties
Converters
Static resource vs Dynamic resource
There is tabcontrol and we have 2 different controls inside it. Now how to load control at run-time. -DataTemplate Selector
DataTemplete vs Control Template
Type of Bindings

Agile and Database
What is Scrum
Estimation
Retrospective meeting
SCrum of Scrum?

What is Self Join? SQL query for it.

Interfaces in C#

Interfaces:

  • Interfaces are contracts that defines the signature of functionality.
  • Interfaces are useful when creating hierarchy of unrelated classes.

Interface Example:


public interface InterfaceA
{
void Display();
}

public class DerivedA : InterfaceA
{
  public void Display()
 {
    Console.WriteLine("Display Method DerivedA");
 }

 void InterfaceA.Display()
 {
   Console.WriteLine("Display Method InterfaceA DerivedA");
 }
}

There is one Display method and other Display method is explicitly implemented. This works fine and does not throw error.

Two interfaces with same method name:

public interface InterfaceA
{
void Display();
}

public interface InterfaceB
{
void Display();
}

In such case we need to explicitly define the method

public class DerivedA : InterfaceA, InterfaceB
{
public void Display()
{
Console.WriteLine("Display Method DerivedA");
}

void InterfaceA.Display()
{
  Console.WriteLine("Display Method InterfaceA DerivedA");
}

void InterfaceB.Display()
{
  Console.WriteLine("Display Method InterfaceB DerivedA");
}
}

Main Method:

static void Main(string[] args)
{

InterfaceA AObj = new DerivedA();
AObj.Display();

InterfaceB BObj = new DerivedA();
BObj.Display();

Console.ReadLine();
}

Result:

Display Method InterfaceA DerivedA
Display Method InterfaceB DerivedA

Modifying interface/Adding new method to interface:

Ideally interface should not change after it is used. But if there is situation where we need to add new method to interface as example below.  One of the way is mentioned below:

Let say there is interfaceA and we want add new method Print()

public interface InterfaceA
{
void Display();
}

InterfaceA is being used. User wants to add new method to it.

Create new interface with method to be implemented and inherit it from original interface.

public interface InterfaceB : InterfaceA
{
void Print();
}

Now use newly created interface were needed. Don’t change other class implementation.

When to use interfaces:

  • If the functionality you are creating will be useful across a wide range of disparate objects, use an interface.
  • If you are designing small, concise bits of functionality, use interfaces.
  • Interfaces cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

Polymorphism Example

Inheritance without polymorphism

public class BaseA
{
	public void Display()
	{
	 Console.WriteLine('Display Method BaseA');	 
	}
}

public class DerivedA : BaseA
{
//Warning InheritanceExample.DerivedA.Display()' hides inherited member 'InheritanceExample.BaseA.Display()'.
//Use the new keyword if hiding was intended.
	public void Display()
	{
	Console.WriteLine('Display Method DerivedA');
	}
}

public class DerivedB : BaseA
{
	public void Display()
	{
	Console.WriteLine('Display Method DerivedB');
	}
}

Main Method

static void Main(string[] args)
{
	BaseA AObj = new BaseA();
	AObj.Display();

	DerivedA ADerObj = new DerivedA();
	ADerObj.Display();

	BaseA AObj1 = new DerivedA();
	AObj1.Display();
	Console.ReadLine();
}


<strong>Output</strong>

Display Method BaseA
Display Method DerivedA
Display Method BaseA


//Error 1 Cannot implicitly convert type BaseA' to DerivedA'.
//An explicit conversion exists (are you missing a cast?)

DerivedA AObj2 = new BaseA();
AObj2.Display();

//Unable to cast object of type BaseA' to type DerivedA'.

DerivedA AObj2 = (DerivedA)new BaseA();
AObj2.Display();

Using new keyboard:

public class BaseA
{
public virtual void Display()
	{
	Console.WriteLine('Display Method BaseA');
	}
}

public class DerivedA : BaseA
{
	public new virtual void Display()
	{
	Console.WriteLine('Display Method DerivedA');
	}
}

public class DerivedB : BaseA
{
	public new virtual void Display()
	{
	  Console.WriteLine('Display Method DerivedB');
	}
}

Inheritance using polymorphism

public class BaseA
{
	public virtual void Display()
	{
	  Console.WriteLine('Display Method BaseA');
	}
}

public class DerivedA : BaseA
{
	public override void Display()
	{
	Console.WriteLine('Display Method DerivedA');
	}
}

public class DerivedB : BaseA
{
	public override void Display()
	{
	Console.WriteLine('Display Method DerivedB');
	}
}

static void Main(string[] args)
{
	BaseA AObj = new BaseA();
	AObj.Display();

	DerivedA ADerObj = new DerivedA();
	ADerObj.Display();

	BaseA AObj1 = new DerivedA();
	AObj1.Display();

	BaseA AObj2 = new DerivedB();
	AObj2.Display();

	//DerivedA AObj3 = (DerivedA)new BaseA(); Casting error
	// AObj2.Display();
	Console.ReadLine();
}

Output

Display Method BaseA
Display Method DerivedA

Display Method DerivedA
Display Method DerivedB

Object Oriented Programming (OOPs)

Pillars of Object Oriented programming

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

What Is Abstraction?

  • Abstraction is showing only the essential details.
  • In real life we have example like mobile, TV where we show only what is required. For mobile there is screen, port for charging and headset and buttons. For TV we have Screen, On-off button and ports for connection.
  • In terms of Application, We only expose the API’s that other user needs. Internally which other API is used, what is sequence that is hidden from user.
  • Abstraction is achieved using Abstract class and Interface.

What is Encapsulation?

  • Encapsulation is hiding the unessential details. Encapsulation is also called data hiding.
  • Encapsulation is a process of binding or wrapping the data and the functions that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.
  • One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
  • One example of Encapsulation is private methods; clients don’t care about it, You can change, amend or even remove that method  if that method is not encapsulated and it were public all your clients would have been affected.
  • In term of real life example, for mobile how the button functionality is implemented, how validation is done and where data is stored is hidden from user.
  • In term of Application we decide how that API works internally.
  • Encapsulation is achieved using Private modifier.

Example of Abstraction and Encapsulation

Calculator

Add method

  • ValidateNumbers
  • SaveNumbers
  • AddNumbers
  • Result

Abstraction

  • Now you need to decide which API to expose. Whether 1 API is sufficient or multiple API’s will be required.

Encapsulation

  • How that API works is hidden from user.
  • Whether that API validates records first or not
  • How database connection is done
  • How saving of those records is done

What is Inheritance?

  • Inheritance is a way by which user can create a new class called derived class from Base class.
  • Derived class inherits the properties and functions of base class.
  • Inheritance is a way by which we achieve code reusability.
  • For example in real word we have Vehicle class or Shapes class.
  • Vehicle will have Body, Engine, Seats, wheels etc. Common functionality can be kept in base class and derived class will have required functionality.
  • It is moving from generalization to Specialization.

What is Polymorphism?

  • Polymorphism means one name many forms.
  • In other words, “Many forms of a single object is called Polymorphism.”
  • There are 2 types of polymorphism:
    • Static Polymorphism
      • Function Overloading can be done by having
        • Different number of parameters.
        • Different sequence of parameters.
        • Different type of parameters.
      • Operator Overloading
        • Giving different meaning to operator. + operator is used for addition. Same can be used for concatenation.
  • Run-time Polymorphism
    • Achieved using virtual and override keyword.
    • The base class method is overridden in derived class using override keyword.

Polymorphism examples:

Example-1:

  • A Teacher behaves to student.
  • A Teacher behaves to his/her seniors.

Example-3:

Your mobile phone, one name but many forms

  • As phone
  • As camera
  • As mp3 player
  • As radio