Saturday 10 October 2020

C# Interview Questions

 

Q. What is the difference between Static class and Singleton instance?

–> In c# a static class cannot implement an interface. When a single instance class needs to implement an interface for some business reason or IoC purposes, you can use the Singleton pattern without a static class.
–> You can clone the object of Singleton but, you can not clone the static class object
–> Singleton object stores in Heap but, static object stores in stack
–> A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded.

Q. What are extension methods and where can we use them?

Extension methods enables you to add new capabilities to an existing type. You don’t need to make any modifications to the existing type, just bring the extension method into scope and you can call it like a regular instance method.
Extension methods need to be declared in a non-generic, non-nested, static class.

Notes:

  • The difference between a regular static method and an extension method is the special this keyword for the first argument.
  • Extension method cannot be declared on a class or struct.
  • It can also be declared on an interface (such as IEnumerable). Normally, an interface wouldn’t have any implementation. With extension methods, however, you can add methods that will be available on every concrete implementation of the interface
  • Language Integrated Query (LINQ) is one of the best examples of how you can use this technique to enhance existing code.

You can read the implementation of Extension Methods in C# here.

Q. What are the differences between Dispose and Finalize ?

DisposeFinalize
It is used to free unmanaged resources at any time.It can be used to free unmanaged resources held by an object before that object is destroyed.
It is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.It is called by Garbage Collector and cannot be called by user code.
It is implemented by implementing IDisposable interface Dispose() method.It is implemented with the help of Destructors
There is no performance costs associated with Dispose method.There is performance costs associated with Finalize method since it doesn’t clean the memory immediately and called by GC automatically.

 Q) What is the difference between ref and out keywords?


No comments:

Post a Comment

Top Agile Interview Questions & Answers

Top Agile Interview Questions & Answers 1. What is Agile Testing? The first question of agile interview question tests your k...