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.
Category: C#
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 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.
Will code in a Finally statement fire if I return a value in a Try block?
Yes.Finally will always be called whatever happens in try catch block.
What is Early binding and late binding
Early binding also known as compile time polymorphism or method overloading.
In OverLoading – We have same class, different parameters and same method name.
Late binding also known as dynamic polymorphism or method overriding and is achieved through parent and child classes by virtual keyword.
In Overriding – We have same method name,same parameters but different classes.
What is Task in c#
.Net framework provides System.Threading.Tasks.Task class to let you create threads and run them asynchronously.
using System;
using System.Threading.Tasks;
namespace TaskExample
{
public static class TaskProgram
{
public static void Main()
{
Task t = Task.Run(() =>
{
for (int x = 0; x < 50; x++)
{
Console.Write(“Hi “);
}
});
t.Wait();
}
}
}
Yield keyword in c#
The yield keyword effectively creates a lazy enumeration over collection items that can be much more efficient.
Traditional way to return Ienumerator
IEnumerable<int> getnumber()
{
var i = 0; var list = new List<int>();
while (i<5)
list.Add(++i); return list;
}
foreach(var number in getnumber()) Console.WriteLine(number);
Loop with Yield
IEnumerable<int> getnumber()
{
var i = 0;
while (i<5) yield return ++i;
}
foreach(var number in getnumber()) Console.WriteLine(number);
What is Differences between Singleton and static class
Static Class :
- Static class has no instance
- We can not implement interface.
- A static class can not have instance members, only static member it can have.
Singleton class :
- We have a single instance of class.
- you can implement interfaces or derive from other classes.
- A singleton class can have instance members,