What is Content negotiation in web api?

Content negotiation mean get data in a format that client need..

content negotiation is the mechanism that is used for serving different representations of a resource at the same URI

We need to tell the service by defining in Request header.For example we want a service to return data in XML format and Json at another time with same service. So to achieve this we will send corresponding Content-type as below..

For Json

Content-type: application/json

For XML

Content-type: application/xml

When we send these Content-type web api checks the request header and response accordingly with the help of MediaFormatter in .Net.

Similarly getting desired image for mate,document formats,language etc. Can be considered Content negotiation.

Difference between Dictionary and Hashtable in C#

Dictionary:

  • It returns error if we try to find a key which does not exist.
  • It is faster than a Hashtable because there is no boxing and unboxing.
  • Dictionary is a generic type .It is type safe.

Hashtable:

  • It returns null if we try to find a key which does not exist.
  • It is slower than dictionary because it requires boxing and unboxing.
  • Hashtable is not a generic type

If keys datatype is not defined you can use Hashtable.For Type safety you shold use Dictionary see below code lines…

Hashtable objHashTable = new Hashtable();
objHashTable.Add(1, 100); // int
objHashTable.Add(2.99, 200); // float
objHashTable.Add(‘A’, 300); // char
objHashTable.Add(“4”, 400); // string

Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add(“cat”, 2);
dictionary.Add(“dog”, 1);
dictionary.Add(“llama”, 0);
// Compilation Error

What is different between var, object and dynamic in C#.

var :

  1. it should be initialized
  2. We can not pass var in method as argument.
  3. Once the type is initialized it can not be changed. It become a strongly type.
  4. Var type is known at compile time. e.g var x = “strng1”; int len= x.Length, here compiler know that x is string type.

Object:

  1. It is not needed to initialize
  2. We can change and pass any type data in object.
  3. Type is resolved at run-time.

Dynamic:

  1. It is not needed to initialize.
  2. We can pass dynamic as argument in method.
  3. Has overhead on compiler for casting.
  4. Dynamic type is known at run time. e.g var x = “strng1”; int len= x.Length, here compiler uses reflection to know the type of x at run time and then check for length method in string.Dynamic internally uses reflection.