A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.
What is difference between String and string.
There is no difference between string and String as they are just alias. We can use any of them for each other.
What is ref and out in C#
By default the parameters passed in methods are by value.
Ref is bidirectional
Out is unidirectional before c#7 it was needed to get initialized out is also passed by ref
Difference in Controller and ControllerBase Class in
Controler.cs
A base class for an MVC controller with view support.
ControllerBase.cs
A base class for an MVC controller without view support.It is used on Web api in ASP .Net Core.
Following components don’t exist in ASP.NET Core:
1.ApiController class
2.System.Web.Http namespace
3.IHttpActionResult interface
Cross Join Vs Inner Join in SQL Server
- SQL INNER JOIN: It returns the records (or rows) present in both tables, If there is at least one match between columns.
- SQL CROSS JOIN: It returns the Cartesian product of both the tables. Cartesian product means Number of Rows present in Table 1 Multiplied by Number of Rows present in Table 2.
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 WCF and Web API
WCF:
- We can use Http,Tcpip,msmq protocol.
- It is recommended when we need transaction or need to develop chat services etc.
- WCF need lot of configuration.
Web API:
- It uses only Http protocol.
- It is recommended when service to be consumed on web,mobile or on low bandwidth devices.
- We don’t need too much configuration like WCF.
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 :
- it should be initialized
- We can not pass var in method as argument.
- Once the type is initialized it can not be changed. It become a strongly type.
- 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:
- It is not needed to initialize
- We can change and pass any type data in object.
- Type is resolved at run-time.
Dynamic:
- It is not needed to initialize.
- We can pass dynamic as argument in method.
- Has overhead on compiler for casting.
- 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.
What is the difference between String.Empty and “” in c#
String x= “”;
String x= String.Empty;
String.Empty will assign reference of memory each time. When you provide value to variable memory is allocated to that variable.
In other case each time memory will be allocated.