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: OOPS
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
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.
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,
Default access modifier of class,interface and property
Default modifier for class internal.
Default modifier for interface public.
Default modifier for property is private.
Is it compulsory to override a virtual method
No, it is not compulsory to override, it is optional.