Webinar Alert : Mastering  Manual and Automation Testing! - Reserve Your Free Seat Now

- Dotnet Blogs -

C# Interview Questions and Answers

C# Interview Questions and Answers

C# is a computer language that runs on the .NET Framework. It is type-safe object-oriented which is not only simple, but also an elegant language that enables developers to build a lot of secure and robust applications like Windows client applications, XML Web services, distributed components, client-server applications, database applications, and a lot more. Many advanced options like advanced code editor, convenient user interface designers, integrated debugger, and many other tools are provided by Visual C# to make it easier for the developers to develop applications based on the C# language and the .NET Framework.

C# alternately referred as C sharp was developed by Microsoft. The syntax that uses curly-brace of C# is instantly recognizable as it is a language based on C, C++ or Java. Developers who have the knowledge of any of these languages are able to work in C# within a very short time. The nullable value types, enumerations, delegates, lambda expressions and direct memory access are the powerful features provided by C#, which simplifies C++ and are add-ons that are not available in Java.

We have compiled a list of top C# interview questions which are classified into 5 sections, namely:

  • Basic C# Interview Questions
  • OOP C# Interview Questions
  • Encapsulation C# Interview Questions
  • C# Interview Questions For Fresher
  • C# Interview Questions For Experienced

C# supports generic methods and types. C# provides increased type safety and performance, and using iterators it enables implementation of collection classes that define custom iteration behaviours. With the help of Language-Integrated Query (LINQ) expressions, C# makes a strongly-typed query a first-class language construct.

C# Interview Questions

  1. Explain the fundamental concepts of OOP?
  2. What are the benefits of using C#?
  3. Describe the basic construction of the C# program.
  4. What is an Interface in C#?
  5. What are the various types of classes in C#?
  6. Write a C# program that outputs “All The Best”.
  7. Name any three ways to pass parameters to a method in C#?
  8. How is operator Overloading achieved in C#? Explain for example?
  9. Differentiate between object type and dynamic type variables.
  10. What is encapsulation? How is encapsulation handled in C#?
  11. Predict the output of the code: [code lang="plain"]delegate void Iterator(); static void Main() { List iterators = new List(); For (int i = 0; i < 15; i++) { iterators.Add(delegate { Console.WriteLine(i); }); } Foreach (var iterator in iterators) { iterator(); } } [/code]
  12. What are generics in C#?
  13. What is the difference between "dispose of" and "finalize" variables in C#?
  14. Differentiate between Array and ArrayList in C#?
  15. What is an Escape Sequence?
  16. What is Reflection in C#?
  17. How will you know what version of the framework is your application targeting?
  18. Differentiate between Static class and Singleton instance?
  19. What are C# I/O Classes? What are the commonly used I/O Classes?
  20. Differentiate between Managed and Unmanaged code.
  21. Explain the process of Code compilation in C#.
  22. Elucidate the difference between Virtual method and Abstract method?
  23. Explain what is Namespaces in C#.
  24. How is Exception Handling executed in C#?
  25. What is a Deadlock?
  26. What is Serialization?
  27. How to use Nullable Types in .NET?
  28. What are extension methods and where are they used?
  29. How to implement the Singleton design pattern in C#?
  30. What is the difference between the “throw” and “throw ex” in .NET
  31. What is the use of the volatile keyword?
  32. What is an async method of C#?
  33. Differentiate between Object pooling and Connection pooling in C#?
  34. Can you serialize hashtables and Why?
  35. What is?PDB file?

Basic C# Interview Questions and answers

For Windows, C# is currently the most used languages in the development of third-party apps. It also is a very popular language for mobile devices, including the Windows Phone. The job opportunities have spurred with the growth of Windows, and with the increase in vacancies the completion increases. Down we have categorized some important questions for the beginners and experienced candidates, which will help you crack the interview.

Q1). Explain the fundamental concepts of OOP?

There are four fundamental concepts of Object Oriented Programming, they are:

  • Encapsulation– The Internal portrayal of An object is avoided from the view outside of the object's definition. Just the required data can be gotten to through whatever remains of the information execution, it is covered up.
  • Abstraction– It is a procedure of recognizing the basic conduct and information of a question and disposing of the immaterial points of interest.
  • Inheritance–It is the capacity to make new classes from another class. It is finished by accessing, altering and broadening the conduct of articles in the parent class.
  • Polymorphism– The name implies, one name, numerous structures. It is accomplished by having numerous strategies with a similar name yet with a unique usage.

Q2). What are the benefits of using C#?

Here are a few advantages of using C#-

  • C# is an object-oriented language that enables you to make secluded viable applications and reusable codes.
  • Automated Garbage Collection.
  • No Problem in case of Memory Leak.
  • Easy-to-Develop.
  • Cross Platform.
  • Faster Integration.
  • More Legible Coding.
  • The scarcity of Choice.

Q3). Describe the basic construction of the C# program.

A typical C# program consists of a namespace declaration, methods, attributes, a class, the main method, statements, expressions, and comments. 

Q4). What is an Interface in C#?

An Interface is a class with no execution. The main thing that it contains is the declaration of techniques, properties, and events.

Q5). What are the various types of classes in C#?

There are different types of class in C#, they are:

  • Partial class– Allows its individuals to be separated or imparted to numerous .cs records. It is signified by the keyword Partial.
  • Sealed class–It is a class which can't be acquired. To get to the individuals from a fixed class, we have to make the object of the class. It is signified by the keyword Sealed.
  • Abstract class–It is a class whose objects can't be instantiated. The class must be acquired. It ought to contain something like one technique. It is indicated by the keyword abstract.
  • Static class– It is a class which does not permit legacy. The individuals from the class are additionally static. It is indicated by the watchword static. This watchword advises the compiler to check for any coincidental occurrences of the static class.

Q6). Write a C# program that outputs “All The Best”.

[code lang="plain"]using System; namespace AllTheBestApplication { class AllTheBest { static void Main(string[] args) { Console.WriteLine("Hello World"); Console.ReadKey(); } } } [/code]

Read: Microsoft .Net Software Developer Salary in USA

 

Q7). Name any three ways to pass parameters to a method in C#?

  • Value parameters:The passing of a parameter to a method by value. A new storage location for the value parameter is created while using the method. The argument does not get affected when changes are made to the value parameter by using this method.
  • Reference parameters: Here instead of creating a new storage location for the parameter, the method accesses the memory location of the argument and passes it as a parameter. Passing of a parameter to a method by reference can be achieved by using the ref keyword. The argument gets affected when changes are made to the value parameter by using this method.
  • Output parameters:In this method the out keyword allows a method to return two values from a function. It is similar to passing a reference parameter, but in this case, data is being transferred out.

OOP C# Interview Questions

Q8). How is operator Overloading achieved in C#? Explain for example?

In C# most of the built-in operators are available and can be overloaded or redefined using the operator keyword. The example below explains the syntax used to implement the addition operator(+) for a user-defined class. [code lang="plain"]public static Rectangle operator+ (Rectangle b, Rectangle c) { Rectangle rectangle = new Rectangle(); rectangle.length = b.length + c.length; rectangle.breadth = b.breadth + c.breadth; rectangle.height = b.height + c.height; return rectangle; } [/code]

Q9). Differentiate between object type and dynamic type variables.

Dynamic type variables handle type checking at the runtime. Object type variables handle type checking during compile time.

Q10). What is encapsulation? How is encapsulation handled in C#?

Encapsulation is a classic object-oriented design principle. Encapsulation reduces coupling between objects and increases maintainable code. It involves enclosing objects within a logical pack by limiting access to implementation details. In C# encapsulation is achieved through the access specifiers public, private, protected, internal, and protected internal.

Q11). Predict the output of the code:

[code lang="plain"]delegate void Iterator(); static void Main() { List iterators = new List(); For (int i = 0; i < 15; i++) { iterators.Add(delegate { Console.WriteLine(i); }); } Foreach (var iterator in iterators) { iterator(); } } [/code] the number 15 is printed fifteen times.

Explanation– The program tests the developer on their knowledge working with loops and delegates. As the delegate is added within the for loop, and because the delegate is the only reference for the variable instead of the value itself, the loop sets the value of the variable I to 15 before it is called within each delegate.

Q12). What are generics in C#?

Generics allow a developer to define a class or method that can work with virtually any data type. It works by delaying specification of the programming element data types until they are needed. The main features of generics are:

  • They make the C# code reusable, type-safe, and optimize the performance.
  • Generics allow the developer to create generic classes, methods, events, delegates, and interfaces in the System.Collections.Generic namespace.
  • At runtime using reflection, generics give the real-time information on the types used in a generic data type.

Q13). What is the difference between "dispose of" and "finalize" variables in C#?

Dispose method uses an interface as “IDisposable" interface and it will free up all managed and unmanaged codes like database connection, files etc. Dispose of the method which is called explicitly. Finalize, unlike dispose method finalize is called internally. It is called by the garbage collector and cannot be called directly from the code. 

Q14). Differentiate between Array and ArrayList in C#?

Array ArrayList
The same data type of values or elements are stored in the array Values of different data types are stored in ArrayList.
Arrays will use the fixed length ArrayList does not use fixed length

Encapsulation C# Interview Questions

Q15). What is an Escape Sequence?

An Escape grouping is signified by an oblique punctuation line (\). The oblique punctuation line shows that the character that tails it ought to be deciphered actually or it is a unique character. An escape sequence is considered a solitary character.

Q16). What is Reflection in C#?

Reflection is the capacity of a code to get to the metadata of the assembly amid runtime. A program reflects upon itself and utilizes the metadata to educate the user or adjust its conduct. Metadata alludes to data about objects, techniques, methods.

Q17). How will you know what version of the framework is your application targeting?

You can define “targetFramework” in Web.config file to specify the framework version. It is introduced in Asp.net 4.0. [code lang="plain"]<?xml version="1.0"?> <configuration> <system.web> <compilation targetframework="4.0" /> </system.web> </configuration> [/code]

Q18). Differentiate between Static class and Singleton instance?

Static Class Singleton instance
A static class cannot implement an interface A singleton instance can implement an interface
Cloning of objects is not possible Cloning of objects is possible
Stores in stack Stores in heap
Generally initialized when it is first loaded Can be initialized lazily or asynchronously

The Singleton pattern is considered an anti-pattern because they are not easy to handle with unit test and their instantiation cannot be controlled, Memory allocated to a Singleton cannot be freed. In the multithreaded environment, access to the singleton object should be guarded in the multithreaded environment, and it is hard to test as it promotes tight coupling between classes.

Q19). What are C# I/O Classes? What are the commonly used I/O Classes?

C# has System.IO namespace, comprising of classes that are utilized to perform different tasks on records like making, erasing, opening, closing and so on. 

Read: What Are The Roles & Responsibility Of A .Net Developer

Q20). Differentiate between Managed and Unmanaged code?

Managed Code Unmanaged Code
Managed code is a code which is executed by CLR (Common Language Runtime) i.e all application code is dependent on.Net Platform. It is considered as overseen in view of them. The net structure which internally utilizes the junk collector to clear up the unused memory. Unmanaged code is any code that is executed by application runtime of some other structure separated from.Net. The application runtime will deal with memory, security and other execution activities.

Q21). Explain the process of Code compilation in C#.

There are four steps in the procedure involved in code compilation which include:

  • Compiling the source code into Managed code by C# compiler.
  • Combining the recently made code into gatherings.
  • Loading the Common Language Runtime(CLR).
  • Executing the gathering by CLR.

C# Interview Questions For Fresher

Q22). Elucidate the difference between Virtual method and Abstract method?

Virtual Method Abstract Method
A Virtual method dependably has a default execution. Nonetheless, it very well may be abrogated in the determined class, however not obligatory. It tends to be superseded utilizing override keyword. An Abstract method does not have a usage. It lives in the abstract class. It is obligatory that the determined class actualizes the dynamic strategy. An override keyword isn't essential here however it tends to be utilized.

Q23). Explain what is Namespaces in C#.

They are utilized to compose substantial code ventures. "System" is the most broadly utilized namespace in C#. We can make our very own namespace and utilize one namespace in another, which are called Nested Namespaces. They are indicated by the keyword "namespace". 

Q24). How is Exception Handling executed in C#?

Exception handling is done using four keywords in C#:

  • try– Contains a block of code for which a special exception will be checked.
  • catch– It is a program that gets an exemption with the assistance of special exception handler.
  • finally-It is a block of code written to execute in any case whether a special exception is gotten to or not.
  • Throw– Throws a special exception when an issue happens. 

Q25). What is a Deadlock?

A Deadlock is where a procedure can't finish its execution since at least two procedures are waiting for each other to wrap up. This generally happens in multi-threading.

Q26). What is Serialization?

Serialization is a procedure of changing over a code to its twofold configuration. When it is changed over to bytes, it tends to be effortlessly put away and kept in touch with a plate or any such capacity gadgets. Serializations are chiefly helpful when we would prefer not to lose the first type of the code and it tends to be recovered whenever you want at a later stage.

Q27). How to use Nullable Types in .NET?

In order to ensure a variable is valid before using it, nullable types help by creating a value type variable that can be marked as valid or invalid. Regular value types are called non-nullable types.

  • Between on-nullable type and its nullable version an implicit conversion occurs.
  • Between nullable type and its non-nullable version an explicit conversion.

C# Interview Questions For Experienced

Q28). What are extension methods and where are they used?

Extension methods enable adding new capabilities to an existing type. No modifications are needed to the existing type, just bringing the extension method into the scope and calling it like a regular instance method will serve the purpose. Extension methods have to be declared in a non-generic, non-nested static class.

Read: Top 30 Advanced Microsoft Power BI Interview Questions & Answers

Q29). How to implement the Singleton design pattern in C#?

Implementing Singleton in C# can be done in numerous ways:

  • Standard Singleton Implementation
  • Double checked locking
  • Using Generics
  • Early Instance Creation
  • Fully Lazy Instantiation
  • using Lazy type

Q30). What is the difference between the “throw” and “throw ex” in .NET

Throw throw ex
throw re-throws the exception that was caught throw ex-throws the same exception.
Preserves the stack trace Resets the stack trace to that method

Unless the requirement is to reset the stack trace (i.e. to shield public callers from the internal workings of the library), it is better to use the throw as it facilitates viewing the exception origin. 

Q31). What is the use of the volatile keyword?

The volatile keyword redirects to a field and indicates it could be modified by multiple threads that are executed at the same time. Fields declared volatile are not optimized from compilers. It automatically assumes access by a single thread and makes sure the presence of most up-to-date value of fields at all times. The volatile modifier is usually used without using the lock statement to serialize access for a field that is accessed by multiple threads.

The volatile keyword can be applied to fields of the following types:

  • Reference types.
  • Pointer types
  • Integral types such as byte, byte, short, short, int, int, char, float, and bool.
  • An enum type with an integral base type.
  • Generic type
  • IntPtr and UIntPtr. 

Q32). What is an async method of C#?

An async is a method that returns to the calling method before completing its work completely. It then completes its work while the calling method continues its execution.

Characteristics of an async method:

  • Before the return type, an async method must have the async keyword in its method header.
  • The modifier does not do anything more than a signal that the method contains one or more await expressions.
  • It contains one or more await expressions representing tasks that can be done asynchronously.
  • It must have either of the following three return types.
    • void: When the calling method wants the async method to execute but doesn't require any further interaction along with it.
    • Task:When from the async method, the calling method doesn’t need a return value but needs to be able to check on the async method’s state.
    • Task:When the calling method receives a value of type T back from the call, the return type of the async method used must be Task.
  • Any number of formal parameters can be present in the async method but it cannot be out or ref parameters.
  • The name of an async method should end with the suffix Async.
  • Other Methods that act as an async object are lambda expressions and anonymous methods. 

Q33). Differentiate between Object pooling and Connection pooling in C#?

The object pool implementation increases the size of the pool to the maximum. Instead of throwing an exception, when the maximum is reached, it makes the client wait until an object is returned to the pool and then gives the newly returned object to the waiting client. The connection pool implementation does not require, a maximum size parameter – it will keep creating new connections as and when required in order to meet demand (eventually it will hit some system limit resulting in the failure in some way that isn’t specified.

Q34). Can you serialize hashtables and Why?

Serializing is not possible hashtables, because any object that implements the IDictionary interface, serialization is not allowed by .NET Framework.

Q35). What is?PDB file?

Program Data Base PDB is a repository (persistent storage as databases) to maintain the information required to run the program in debug mode. PDB contains a lot of relevant information while debugging the code.



fbicons FaceBook twitterTwitter lingedinLinkedIn pinterest Pinterest emailEmail

     Logo

    JanBask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


  • fb-15
  • twitter-15
  • linkedin-15

Comments

Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

-0 day 12 Oct 2024

QA Course

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

14 days 26 Oct 2024

Salesforce Course

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

-0 day 12 Oct 2024

Business Analyst Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst Course

Upcoming Class

-0 day 12 Oct 2024

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

-0 day 12 Oct 2024

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

-0 day 12 Oct 2024

DevOps Course

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

6 days 18 Oct 2024

Hadoop Course

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

13 days 25 Oct 2024

Python Course

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

7 days 19 Oct 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

-0 day 12 Oct 2024

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

34 days 15 Nov 2024

 Tableau Course

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

13 days 25 Oct 2024

Search Posts

Reset

Receive Latest Materials and Offers on Dotnet Course

Interviews