Skip to main content

Top 15 C# Interview Question

 



C# is a Microsoft-developed, general-purpose, object-oriented programming language.
It is one of the most flexible programming languages with a powerful runtime, access to a huge amount of library and platform functionality, and a strong set of abstractions.

Using C#, you are prone to create web applications or gaming apps. C# has some fantastic features like automatic garbage collection, interfaces, etc. which help build better applications.

C# is such a widely-used programming language, a plethora of big and small organizations base their products using it. So, prepare yourself with basic and advanced level C# questions to ace the interviews. 


Top 15 C# Interview Questions and Answers.

   
          Now, let us take a look at the top C# interview questions that you might face.
   

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?

An array is a collection of similar variables clubbed together under one common name. While ArrayList is a collection of objects that can be indexed individually. With ArrayList you can access a number of features like dynamic memory allocation, adding, searching, and sorting items in the ArrayList.
 

8.What are Generic collections?

In C# collections, defining any kind of object is termed okay which compromises C#’s basic rule of type-safety. Therefore, generics were included to type-safe the code by allowing re-use of the data processing algorithms. Generics in C# mean not linked to any specific data type. Generics reduce the load of using boxing, unboxing, and typecasting objects. Generics are always defined inside angular brackets <>.


9.How do we handle exception in C#?

Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: trycatchfinally, and throw.
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# ref keywords pass arguments by reference and not value. To use the ‘ref’ keyword, you need to explicitly mention ‘ref’. 
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. 
12.What are Properties in C#?
Properties in C# are public members of a class where they provide the ability to access private members of a class. The basic principle of encapsulation lets you hide some sensitive properties from the users by making the variables private. The private members are not accessible otherwise in a class. Therefore, by using properties in C# you can easily access the private members and set their values. 


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?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts. Events are used for inter-process communication.
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.
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.
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.

15. What are Indexers in C#?

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

Popular posts from this blog

4 Ways to Prove the Earth Is Round

Compare shadows: Advertisement The first person to estimate the circumference of the Earth was a Greek mathematician named Eratosthenes, who was born in 276 B.C. He did so by comparing shadows case on the day of the summer solstice in what is today Aswan, Egypt, with the more northerly city of Alexandria. At noon, when the sun was directly overhead in Aswan, there were no shadows. In Alexandria, a stick set in the ground cast a shadow. Eratosthenes realized that if he knew the angle of the shadow and the distance between the cities, he could calculate the circumference of the globe. On a flat Earth, there wouldn't have been any difference between the length of the shadows at all. The sun's position would be the same, relative to the ground. Only a globe-shaped planet explains why the sun's position should be different in two cities a few hundred miles apart.                            Go climb a tree: This is another ...

Chemical reaction and equation

  Chemical reactions -   The transformation of chemical substance into a new chemical substance by making and breaking of bonds between different atoms is known as Chemical Reaction.  Signs of a chemical reaction These factors denote that a chemical reaction has taken place- change of state of substance, change of color of substance,evolution of heat, absorption of heat, evolution of gas and evolution of light. Chemical Equation:   The representation of chemical reaction by means of symbols of substances in the form of formulae is called chemical equation.  E.g. - H 2  + O 2  ⇒ H 2 O                        Balanced Chemical Equation:   A balanced chemical equation has number atoms of each element equal on both left and right sides of the reaction.                                    *No...

Meteorological Data Analysis

Is there any change due in weather to global warming in of Finland by using Data analytics                                       Effect of  global warming  “Has the Apparent temperature and humidity compared monthly across 10 years of the data indicate an increase due to Global warming” To find whether the average Apparent temperature for the month of a month say April starting from 2006 to 2016 and the average humidity for the same period have increased or not. step-1  Importing of libraries and Dataset.   import libraries step-2   over Look at the dataset. step-3 Cleaning the Dataset step-4 Plotting a graph of  the following Dataset >  Firstly  plot the  graph whole dataset for all months  Graph for all month >  Now    plot graph for a specific month(April) .   Graph for  month of April Conclusion: As we ca...