Top 15 C# Interview Questions and Answers.
1.Explain the difference between .NET and C#?
C# is a programming language and .NET is a framework .NET has Common Language Runtime (CLR), which is a virtual component of .NET framework.
C# is a part of .NET and has the following features −
- Boolean Conditions
- Automatic Garbage Collection
- Standard Library
- Assembly Versioning
- Properties and Events
- Delegates and Events Management
- Easy-to-use Generics
- Indexers
- Conditional Compilation
- Simple Multithreading
- LINQ and Lambda Expressions
- Integration with Windows
2.What is IL (Intermediate Language) code?
Intermediate language (IL) is an object-oriented programming language designed to be used by compilers for the .NET Framework before static or dynamic compilation to machine code.
IL(Intermediate language) code is a partially compiled code .JIT(Just in Time compiler compiled intermediate language code to native code (machine code).
3.What is CLR(Common Language Runtime)?
.NET CLR is a runtime environment that manages and executes the code written in any .NET programming language. CLR is the virtual machine component of the .NET framework.
The main components of CLR are :
- Common type system
- Common language speciation
- Garbage Collector
- Just in Time Compiler
- Metadata and Assemblies
4.What is Managed and Unmanaged code?
Any language that is written in the .NET framework is managed code. Managed code use CLR.
Benefits: Provides various services like a garbage collector, exception handling, etc.
The code developed outside the .NET framework is known as unmanaged code. Applications that do not run under the control of the CLR are said to be unmanaged.
They don’t provide services of the high-level languages and therefore, run without them. Such an example is C++.
5.Explain the importance of Garbage collector?
Garbage collection is the process of freeing up memory that is captured by unwanted objects. When you create a class object, automatically some memory space is allocated to the object in the heap memory. Now, after you perform all the actions on the object, the memory space occupied by the object becomes waste. It is necessary to free up memory. Garbage collection happens in three cases:
- If the occupied memory by the objects exceeds the pre-set threshold value.
- If the garbage collection method is called
- If your system has low physical memory
6.Explain Boxing and Unboxing?
The two functions are used for typecasting the data types:
Boxing: Boxing converts value type (int, char, etc.) to reference type (object) which is an implicit conversion process using object value.
Unboxing: Unboxing converts reference type (object) to value type (int, char, etc.) using an explicit conversion process.
7.Difference Between array and arraylist?
8.What are Generic collections?
9.How do we handle exception in C#?
try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.
catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
finally − The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.
throw − A program throws an exception when a problem shows up. This is done using a throw keyword.
10.What are extension methods in C#?
Extension methods help to add new methods to the existing ones. The methods that are added are static. At times, when you want to add methods to an existing class but don’t perceive the right to modify that class or don’t hold the rights, you can create a new static class containing the new methods. Once the extended methods are declared, bind this class with the existing one and see the methods will be added to the existing one.
11.What are the differences between ref and out keywords?
C# out keywords pass arguments within methods and functions.
‘out’ keyword is used to pass arguments in a method as a reference to return multiple values. Although it is the same as the ref keyword, the ref keyword needs to be initialized before it is passed. Here, The out and ref keywords are useful when we want to return a value in the same variables that are passed as an argument.
The values can be easily assigned using get and set methods, also known as accessors. While the get method extracts the value, the set method assigns the value to the variables.
13.What are events?
The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the event. This is called the publisher class. Some other class that accepts this event is called the subscriber class. Events use the publisher-subscriber model.
A publisher is an object that contains the definition of the event and the delegate. The event-delegate association is also defined in this object. A publisher class object invokes the event and it is notified to other objects.
A subscriber is an object that accepts the event and provides an event handler. The delegate in the publisher class invokes the method (event handler) of the subscriber class.
14.What are difference between abstract class and interface ?
- Abstract classes are classes that cannot be instantiated i.e. that cannot create an object. The interface is like an abstract class because all the methods inside the interface are abstract methods.
- Surprisingly, abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods.
- Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface, there is no such need.
Indexers are called smart arrays that allow access to a member variable. Indexers allow member variables using the features of an array. They are created using the Indexer keyword. Indexers are not static members.
For ex. Here the indexer is defined the same way.
Comments
Post a Comment