Top C# Interview Questions

C#ClassStruct
A class is a reference type, while a struct is a value type. A class is allocated on the heap, and structs are allocated on the stack. Structs cannot have a default constructor and cannot be inherited.

C#PolymorphismOOP
Polymorphism in C# is the ability of a single method or operator to work with different data types. It can be achieved through method overloading and method overriding.

C#Virtual KeywordInheritance
The 'virtual' keyword is used to declare a method or property that can be overridden in a derived class. It allows for polymorphic behavior where derived classes can modify the implementation of base class methods.

C#Static KeywordClass Members
The 'static' keyword is used to declare class members that belong to the class itself rather than instances of the class. It can be used with fields, methods, properties, and constructors.

C#EqualityEquals MethodOperators
'==' is used for reference comparison for reference types and value comparison for value types, while '.Equals()' is a method that compares the values of two objects and can be overridden for custom behavior in reference types.

C#Abstract ClassInterfaceOOP
An abstract class can have method implementations and fields, whereas an interface can only declare method signatures and properties. A class can implement multiple interfaces but can only inherit from one abstract class.

C#New KeywordObject Creation
The 'new' keyword is used for two main purposes: to create objects of a class, and to hide a member of the base class when it is inherited in a derived class (method hiding).

C#Ref ParameterOut ParameterMethod Parameters
Both 'ref' and 'out' are used to pass parameters by reference, but 'ref' requires the variable to be initialized before being passed to the method, while 'out' does not require initialization and is used when the method is expected to assign a value to the parameter.

C#Using StatementResource Management
The 'using' statement is used for automatic disposal of objects that implement the IDisposable interface, ensuring that resources such as file handles or database connections are released properly.

C#Nullable TypesNull Handling
A nullable type is a value type that can also represent null. It is defined by appending '?' to the value type, for example, 'int?' allows storing null as well as integer values.

C#DelegateEvent Handling
A delegate is a type-safe function pointer, used to define methods that can be called asynchronously or passed as arguments. It allows methods to be passed as parameters, and called later.

C#InterfaceAbstract ClassOOP
An interface only contains method signatures and properties, while an abstract class can have method implementations as well. A class can implement multiple interfaces but can only inherit from one abstract class.

C#Shallow CopyDeep CopyObject Cloning
A shallow copy copies the object’s top-level values, but not the nested objects, whereas a deep copy duplicates all nested objects recursively.

C#Method OverloadingPolymorphism
Method overloading is the ability to define multiple methods with the same name but different parameter lists. It is determined by the number, type, or order of the parameters.

C#Is OperatorAs OperatorType Checking
'is' is used to check if an object is of a specific type, whereas 'as' is used to attempt a cast to a specific type and returns null if the cast fails.

C#Method OverridingInheritance
Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class. It is done by marking the base method as virtual and the derived method as override.

C#Sealed KeywordInheritance
The 'sealed' keyword is used to prevent a class from being inherited. It can also be applied to methods to prevent them from being overridden in derived classes.

C#This KeywordInstance Reference
The 'this' keyword refers to the current instance of a class. It is used to access instance members and differentiate between method parameters and class members with the same name.

C#Default KeywordDefault Values
The 'default' keyword is used to get the default value of a type. It returns a zero value for value types and null for reference types.

C#Static ConstructorClass Initialization
A static constructor is used to initialize the type itself, rather than an instance of the type. It is called only once before any static members are accessed or any static methods are called.

C#PropertiesEncapsulation
Properties in C# are members of a class that provide a way to read, write, or compute the value of a private field. They are defined using 'get' and 'set' accessors.

C#ListArrayCollections
An Array has a fixed size, whereas a List is dynamic and can grow or shrink in size. Lists also provide more flexibility and methods compared to arrays.

C#LINQQuerying
LINQ (Language Integrated Query) is a feature in C# that allows you to query and manipulate data from different sources such as arrays, lists, databases, XML, and more using a unified syntax.

C#Garbage CollectionMemory Management
Garbage Collection is an automatic memory management feature in C#. It helps in reclaiming memory occupied by objects that are no longer in use by the application.

C#Void KeywordReturn Types
The 'void' keyword is used to indicate that a method does not return a value. It is typically used for methods that perform an action but do not return any result.

C#Exception HandlingError Management
Exception handling in C# is the process of detecting and handling runtime errors in a program using try, catch, and finally blocks. This helps to gracefully handle unexpected errors.