SOOPRO Pathshala provides C# Interview Questions and Answers
C# is pronounced "C-Sharp". It is an object-oriented programming language created by Microsoft that runs on the .NET Framework.
The role of C# as a programming language is used to create desktop apps, mobile apps, web apps, websites, and web services.
Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic.
Managed code is executed by the Common Language Runtime (CLR) of the .NET Framework.
unmanaged code is executed by the Operating System (OS).
C# is an object-oriented programming language, whereas C is a procedural programming language.
C# is used for desktop and mobile apps as well as services, where as C is best suited for hardware apps and system programming.
C#has 87 keywords, where as C draws on just 32 different keywords.
An object is a real-world entity.
A class is a user-defined blueprint architecture, which is used to contains data members and member functions.
A method is a set of statements which perform some action.
In C#, a structure is a composite type of data consisting of various data types, including methods, fields, constructors, constants, properties, indexers, operators, and even other structures.
When a project is developed, C# source code is compiled into Intermediate Language (IL). IL is a set of instructions that further converted by JIT compiler to machine code.
File handling is the process of saving information to the disk of external storage. The saved file contains bytes of data and is available for retrieval at a later date.
Control statements are used to control the flow of execution of program.
Garbage collection is the process of managing memory in an application. The garbage collector automatically disposes of memory that is no longer used to make memory available for new allocations.
A constructor is a special method of a class or structure that initializes data members or object of class
Destructor is an instance member function that is invoked automatically whenever an object is going to be destroyed. Destructors are also referred to as finalizers.
An array is a collection of similar elements.
Constants are fixed values that cannot be modified or changed.
C# indexers are usually known as smart arrays.
1. Abstract classes: These provide a common definition for a base class that other classes can be derived from
2. Static classes: These contain static items that can only interact with other static items.
3. Partial classes:These are portions of a class that a compiler can combine to form a complete class
4. Sealed classes:These cannot be inherited by any class but can be instantiated
Circular references, also known as circular dependencies, occur when two or more components, classes, namespaces, or projects depend on each other directly or indirectly.
Object pooling is a memory optimization technique that reduces the overhead of creating and destroying objects frequently
There are generally considered to be three main types of control statements
1. Conditional statements which enable you to branch to different sections of code
2. Looping statementswhich enable you to loop through connections or perform the same series of operations repeatedly until a specified condition is met
3. Jump statementswhich enable control of flow to be shifted to another section of code
Defining multiple methods having same name with different signatures in a same class is called method overloading.
Method overloading improves the readability of the program by reducing the number of names associated with a specific action.
Boxing is used to convert a value type to a reference type.
Unboxing is used to convert a reference type to a value type.
The ref keyword is used when you want to pass a variable by reference to a method.
The out keyword is used when you want to pass a variable to a method for output purposes
Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface.
Namespaces in C# are used to organize too many classes so that it can be easy to handle the application.
Nullable types are used to represent an undefined value of an underlying type. It essentially means ‘no value’ and is generally used when no data is available for the field.
In C#, serialization is the process of converting an object into a stream of bytes for storage on a memory, database, or file.
A string object is immutable, meaning that it cannot be changed after it’s created.
A string builder object is mutable and can be modified as the developer sees fit.
In C#, reflection is used to obtain metadata on types at runtime. In other words, it allows developers to retrieve data on the loaded assemblies and the types within them.
It’s implemented using a two-step process. First, you get the type object. Second, you use the type to browse members, such as methods and properties.
An Array is strongly-typed, meaning it only stores the same type of data.
An ArrayList is a non-generic collection type, meaning it can store multiple types of data
An Array stores a fixed number of elements.
An ArrayList features a variable number of elements and can continually be added to
An Array cannot accept null values, whereas an ArrayList can
Each type of control statement has its own set of syntax used to invoke the statement:
1. Conditional statements include if- else, switch-case
2. Iteration statements include for, while, do-while and foreach
3. Jump statements include break, continue, return, and goto
Multithreading, or threading, can be a good way to improve the performance of a program where several operations run simultaneously.
A multicast delegate in C# references multiple target methods. When a multicast delegate is used, all the functions the delegate is pointing to are invoked.
The four fundamental concepts of object-oriented programming can be explained as follows:
Encapsulation is used to show non essential feature and hide essential feature.
Polymorphism is the ability of a type to take on many forms using a single interface
Abstraction is used to show essential part and hide non essential part.
Inheritance is the process where one class derives (or inherits) its attributes and methods from another
In this pattern, a class has only one instance in the program that provides a global point of access to it.
This design pattern can be implemented in a number of ways, using:
Private and parameterless single constructor
Sealed class
Static variable to hold a reference to the single created instance
Early binding occurs at compile-time, whereas late binding occurs at runtime
If derived class defines same method as defined in its base class, it is known as method overriding in C#. It is used to achieve runtime polymorphism.
There are several differences between Const and ReadOnly keywords in C#. These include:
ReadOnly is a constant used at runtime, whereas Const is a constant used at compile-time.
ReadOnly values can be changed, whereas Const values cannot be changed.
ReadOnly cannot be declared inside the method, whereas Const can
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Dependency Injection (DI) is a software design pattern that helps developers build better software. It allows us to develop loosely-coupled code that is easy to maintain.
To handle the problem of circular references in C#, you should use garbage collection.
In C#, a static class is a class that cannot be instantiated.
In C#, exception handling helps detect errors in code at runtime. The process is implemented using four different keywords:
1. Try identifies blocks of code where exceptions are activated.
2. Catch catches the exceptions that have been identified by Try
3. Finally executes a given set of statements depending on whether an exception is thrown out or not
4. Throw A program throws an exception when a problem shows up. This is done using a throw keyword.
Throw exception generally refers to the action of explicitly raising or generating an exception within your code.
Throw clause typically refers to the part of a method signature that indicates which exceptions can be thrown by that method
Interface - An interface in C# defines a contract for classes that implement it. It is a purely abstract concept and can only contain method, property, event, or indexer declarations.
Interfaces cannot contain any implementation code. Classes that implement an interface must provide concrete implementations for all the members declared in the interface.
An abstract class in C# is a class that cannot be instantiated on its own and is meant to serve as a base or blueprint for other classes.
The enum keyword is used to declare an enumeration. It is a primitive data type that is user-defined.
Using a break statement, you can 'jump out of a loop,' whereas using a continue statement, you can 'jump over one iteration' and resume your loop execution.
A property is a member of a class that provides a way to read, write or compute the value of a private field.
The Dispose method releases unmanaged resources, such as file handles or database connections, not automatically managed by the .NET runtime.
The Finalize method is used to perform cleanup operations on an object just before it is garbage collected. Therefore, it is typically implemented in a class that overrides the Object.Finalize method.
C# is an object-oriented, modern programming language that was created by Microsoft. It runs on the .NET Framework.
Garbage collection in C# is an automatic memory management process that helps to release memory used by objects that are no longer needed by the application. .
A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit.
A structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types
The new keyword is to create instances of classes (objects). It allocates memory for the object
Reflection is the process of describing the metadata of types, methods, and fields in a code.
A constant in C# is a value that is known at compile-time and remains unchanged throughout the program's execution
A read-only variable in C# is a variable whose value can be assigned at runtime, typically within a constructor, and once assigned, it cannot be changed.
The primary difference between dispose() and finalize() is that the dispose() must be explicitly invoked by the user and the finalize() is called automatically by the garbage collector when the object is destroyed.
A delegate is a reference type variable that holds the reference to a matching method.
Sealed classes are used to restrict the users from inheriting the class
Multicasting of delegate is an extension of the normal delegate. It helps the user to point more than one method in a single call.
In C# virtual method is a strategy that can be reclassified in derived classes A virtual method is declared in the parent class that can be overridden in the child class.
Multi-threading is a process that contains multiple threads within a single process.
The Hashtable class represents a collection of key/value pairs that are organized based on the hash code of the key.
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5 and Visual Studio 2008.
The beauty of LINQ is it provides the ability to .NET languages(like C#, VB.NET, etc.) to generate queries to retrieve data from the data source.
Because private virtual methods are not accessible outside the class.
Singleton design pattern in C# is a common design pattern. In this pattern, a class has just one in
An event is a notification that generate when some action or changes occur.
Array is a fixed-size data structure that holds elements of the same data type.
ArrayList is dynamic array and grows automatically when new items are added to the collection.
SortedListis implemented as an array of key-value pairs, where the keys are stored in a sorted order.
SortedDictionary is a collection class that provides a dynamically resizable data structure for storing key-value pairs, where the keys are sorted in a specific order
Single line
Multi line
An object is an instance of a class through which we access the methods of that class
A constructor is a member function in a class that has the same name as its class. The constructor is automatically invoked whenever an object class is created.
Jagged array is a container of other arrays. No. of rows in jagged called no. of arrays.
The process of converting an object into a stream of bytes is called Serialization
We can’t use ‘This’ in a static method because we can only use static variables/methods in a static method.
A value type holds a data value within its own memory space. Reference type stores the address of the Object where the value is being stored. It is a pointer to another memory location.
Method overloading is creating multiple methods with the same name with different signatures in the same class.
Protected Internal are accessible within the all classes of same application and in child class of other application.
Method Overriding that enables a subclass to provide a specific implementation for a method that is already defined in its superclass.
Method overloading is a process of creating multiple methods with the same name having different signatures within same class.
Methods can be overloaded using different data types for a parameter, different order of parameters, and different number of parameters.
is operator is used to check the compatibility of an object with a given type, and it returns the result as Boolean.
as operator is used for casting of an object to a type or a class.
.dll: DLLs cannot be executed directly. They need to be referenced by other programs (usually EXE files) that use the functionality provided by the DLL.
.exe: EXE files are executable and can be run directly by the operating system
A console application is an application that can be run in the command prompt in Windows
A namespace used to organize and group related classes, interfaces, structs, enums, and other types into a hierarchical structure.
A keyword is a reserved word that has a predefined meaning and purpose in the programming language.
A variable is a named storage location that holds a value of a specific data type.
A method is a block of code that performs a specific task or operation.
An attribute is a declarative tag that provides additional information about an element in your code, such as a class, method, property, parameter, or assembly.
Type casting in C# refers to converting one data type to another.
An assembly is the primary building block of a .NET Framework application. It is an Output Unit. Assemblies contain MSIL code.
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
Loops are used to execute one or more statements multiple times until a specified condition is fulfilled.