DEV Community

Shahzad
Shahzad

Posted on

c# interview questions

What is C# history?

C# (pronounced "C Sharp") is a programming language that is latest, flexible. It is made by Microsoft. C# is an object-oriented programming language that support four main pillar of OOP Abstraction, Encapsulation, Inheritance, Polymorphism. It run on .NET Framework. It's history start from late 1990s.

2. What are C# main feactures?

C#, which is also called C Sharp, is a programming language that was made by Anders Hjelsberg. C# combines the computational power of C++ and the ease of Visual Basic, Microsoft’s event-driven programming language and environment. C is the building block from which C# is made. Microsoft came out with C# in the year 2000. It was made to meet the growing demand for online applications that Visual Basic (VB) and C++ can’t meet. Because of this, so The programmers who know how to work with C and C++ can quickly learn how to use C# and by using C# you can make website and desktop application very easily in versatile way and you can also use web services that make capable two or more systems to exchange data over network. Data are exchanged either using XML or JSON.

Here, below are some main features of C#

1 - Object-Oriented

2 - Type Safety

3 - Garbage Collection

4 - Cross-Platform Development (C# 9.0 and later)

5 - Managed Code

6 - Rich Standard Library

3. What is Dot Net Framework?

Dot NET Framework is a software development framework for making and running applications on Windows. Dot Framework is part of the .NET platform and provide us a collection of technologies for building website, software and apps for Linux, macOS, Windows, iOS, Android. It has a common runtime (CLR - Common Language Runtime) and a large number of class library that supports more then 60 programming languages, including C#. .NET Framework is the original implementation of .NET. It supports running websites, services, desktop apps, and more on Windows.

4. What is Common Language Runtime (CLR)?

The management of the execution of.NET applications is the responsibility of the CLR. It provides a range of services including garbage collection, exception handling and security. It is a program execution engine that loads and executes the program. It converts the program into native code. It acts as an interface between the framework and operating system. It does exception handling, memory management, and garbage collection. Moreover, it provides security, type-safety, interoperability, and portablility. A list of CLR components are given below:

5. What is Base Class Library (BCL)?

The BCL contains a set of classes, interfaces and value types that provide the essential functionality for your.NET applications. It's about collections, files, networking, security, and more.

6.What are keywords in C#?

Keywords that we use in C# programming language have special meanings and these Keywords as they are special in programming so we cannot used it as identifiers specially like variable name, method name, and class names in you code.

We define vairable with string keyword in C#.

Here, in below example

string - a keyword

fruitName - variable name

Copy Code

// Define a string variable

string fruitName;

7. What are operators in C#?

In C#, operators are symbols or keywords used to perform various operations on data, such as arithmetic calculations, logical comparisons, and assignments.

8. What are data types in C#?

The behavior of storing data in variable are described with data types. It is responsibility of a specific data type to store a specific value. For example number values are store in integer type data types.

Primitive Data Types:

Integral Types:

Floating-Point Types:

Character Type:

Boolean Type:

Reference Data Types:

String Type:

9. What are Variables in C#?

Variables in C# are used for storing data this data can be in shape of number, characters, text and true, false and decimal. for example for example: int - stores integers (whole numbers), without decimals, such as 123 or -123. double - stores floating point numbers, with decimals, such as 19.99 or -19.99 and char store character like 'A' and string store text like "Apple Red!"

10. What is break statement in C#?

In C#, the break statement is a control flow statement that is used to exit a loop or switch statement prematurely. It allows you to terminate the execution of the innermost loop or switch block and continue with the code immediately following the loop or switch statement

11. What is continue statement in C#?

The continue statement is used to skip the current iteration of a loop. This continue statement usually we used with for, while, do-while statement. when continue statement skipped the current iteration then control goes to next iteration. It is typically used within loops to control the flow of execution based on a specific condition.

12. What is goto statement in C#?

In C#, the goto statement allows you to transfer control to a labeled statement within the same method or block of code.

13. What is function in C#

In C#, a function is a block of code that performs a specific task and can be called from other parts of your program. Functions are a fundamental concept in C# and are often referred to as methods.

Functions in C# are known as methods and are defined within a class or struct. They can have parameters, which are variables passed into the method, and they can return a value or be void (returning no value).

14. What is ref parameter in C#

In C#, the ref keyword is used to indicate that a method parameter is a reference parameter.

Reference parameters allow you to pass a reference to a variable as an argument to a method, rather than passing a copy of the variable's value. This means that any changes made to the parameter within the method will affect the original variable outside the method as well.

15. What is out parameter in C#

The out keyword is used to declare output parameters in method or function signatures. The output parameter is a parameter that is used to return multiple values from a method. The output parameters are used to pass data out of a method. But a normal function is used for passing the data inside function.

16. What is in parameter in C#

in is used to state that the parameter passed cannot be modified by the method. An any attempt to modified will give you compile-time error. in parameter was introduced in C# 7.2.

17. What is array in C#

Array is such type of collection that have the capability to stores a fixed-sized elements of the same data type. But have the difference with mixed data types like array List that can store mixed data types.

In array you can store multiple values of the same data type for example you can store multiple values of int data type of same variable and similarly In array you can also access multiple values of the same type under a single variable name.

Suppose we need to record the 7 different numbers. Instead of creating 7 separate variables, we can simply create an array:

18.Can we pass array to function?

A function can receive a parameter. Similarly a function can also receive an array. We can pass an array as parameter to function. In this activity, when we pass an array to a function we actually passes a reference to the array. we give permission to a function that it can access and can modify the elements of the array.

19.What is multidimensional array in C#?

If you have date in the shape of tables, matrices, grids then multidimensional arrays are useful. We declared the multidimensional array by adding commas in the square brackets. We can declare two-dimensional, three-dimensional, four-dimensional array in such a way respectively [,], [, ,], [, , ,].

20. What is jagged array in C#?

In C#, a jagged array is an array of arrays. Unlike rectangular (multidimensional) arrays, jagged arrays allow each element of the outer array to be an array of different lengths. This flexibility makes jagged arrays useful for representing irregular data structures where the inner arrays can have varying sizes. Each row (dimension) has a different length or size in jagged arrays, but in a multidimensional array, each row (dimension) has fixed or the same length because because each row is essentially a separate array.

21. What are params in C#?

In C#, "params" is a keyword used in method parameter to indicate that a method can accept a different number of parameters of the same type. This feature is often used when you want to create methods that can accept an undecided number of parameters.

22. What are the types of classes in C#?

Here, below are types of classes in C#

1 - Abstract class

2 - Partial class

3 - Sealed class

4 - Static class

23. What is class in C#?

A class is basic pillar of OOP that is called when instance of a class is created. A class can have fields, properties, method, constructor and destructor etc. When we create an instance of class. This instance of class is used to access of fields, methods, properties, constructor (constructor can zero argument or multiple argument) and destructor. A class is reference type and stores in heap.

24. What is constructor in C#?

Constructor is special method that called when object/instance of class is created

25. What is destructor in C#?

Destructor is also a special method that called when object/instance of class is destroyed.

26. What is static class in C#?

In C#, a static is a keyward that is used with fields, methods, properties and classes when we make these members static they are globally accessble in all over the application.

Rules for Making Static Class

Here we have defined some rules of making static class.

Declaration

A static class must be declared using the static keyword.

Static Members:

All members (methods, properties, fields) of a static class must also be static.

Instantiation:

Static classes cannot be instantiated; you cannot create an object of a static class.

Instance Members:

Static classes cannot contain instance members (non-static fields or methods).

Static Methods:

A static class can contain static methods, which can be called without creating an instance.

Static Constructor:

A static class can have a static constructor for initialization purposes, which is called automatically before any static members are accessed.

Accessibility:

Static classes are sealed class and therefore, cannot be inherited. A static class cannot inherit from other classes.

27. What is static method in C#?

A Static method is also accessible all over the application globally. When we want that a method will be accessible in all over the application with creating the instance of class then we make it static.

*Rules for making Static Methods
*

Here we have defined some rules of making static method.

Accessibility Modifiers

Static methods can have access modifiers (e.g., public, private) that determine their visibility outside the class

Inheritance:

Static methods are not polymorphic. You cannot override a static method in a derived class; however, you can hide it by declaring a static method with the same name.

No this Keyword:

Inside a static method, you cannot use the this keyword since there is no instance context.

Calling:

You can call static methods using the class name, or from other static methods within the same class without needing to specify the class name.

No Access to Instance Members:

Static methods cannot access instance variables or instance methods directly. They can only interact with static members.

28. What is static constructor in C#?

A non-static class can contain a zero-argument static constructor. It can be defined with the static keyword and without access modifiers like public, private, and protected.

Note:The static constructor gets called when you create an instance for the first time.

Static Constructor Example

Copy Code

// First static constructor and then instance constructor called

Fruit sw1 = new Fruit();

// only instance constructor called

Fruit sw2 = new Fruit();

Rules for making Static Constructors

Here we have defined some rules of making static constructor.

Declaration

A static constructor is defined with the static keyword and does not have any parameters.

Initialization Timing:

It is called automatically before any static members are accessed or any instance of the class is created.

Single Invocation:

The static constructor is called only once per type, regardless of how many instances of the class are created.

No Parameters:

They cannot take parameters, which means they can’t be overloaded.

Inheritance:

If a derived class has a static constructor, the base class's static constructor is called first.

29. What is struct in C#?

A struct a value type and it store it's data in stack. Classes are reference type and it store it's data in heap. The structs are usually used for small data structures. The structs are specially used when we don't need features of classes like inheritance.

Suppose we want to store the name and color of a fruit. We can create two variables: name and color and store value. However, suppose we want to store the same information of multiple fruit.

In this case, creating variables for an individual fruit might be a tedious task. To overcome this we can create a struct that stores name and color. Now, this struct can be used for every fruit.

30. What is enum in C#?

The enum is also called enumeration. It is a value type. Enum is used to define the set of named constant values

31. What is property in C#?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties allow you to define getter and setter methods for private fields.Properties are often used to enforce data encapsulation (mean sensitive data remain hidden from user) and provide a clean and consistent interface to access an object's data.

32. What is inheritance in C#?

You can create a base class (parent class). You can derive a new class (child class) from this existing base class (parent class). When you inherit a child class with base class you also inherit it's members (fields, methods, properties etc.). you can also add these members and also can modify them as needed.Inheritance promotes code reuse, simplifies code maintenance, and improves code organization.

33. How many types of inheritance in C#?

Types of inheritance

There are the following types of inheritance:

1*. Single Inheritance*

In single inheritance, a single derived class inherits from a only single base class. In below diagram, Fruit is parent class that has one to one relationship with orange derive class.So Fruit is Super class and Orange is Sub class.

Single Inheritance : Example

In below example, we have Fruit (base class) that have two methods with named FruitTaste(), FruitColor(). We have inherited Orange derived class with Fruit base class and this Orange class have two methods with named hasTaste() and hasColor() and you can also access Fruit base class methods here in Orange derived class.

2.Multilevel Inheritance

In single inheritance, a derived class inherits from a base class and then this base class also acts as a derived class which is inherits from a another base class. In, below diagram, Blood Orange is dervied class that is inherited with Oranges (derived class) and then Oranges (derived class) is inherited with Fruit (base class).

Multilevel Inheritance : Example

In below example, we have Fruit base class which are inherited with Oranges derived class and this Fruit class has method with named hasTaste() and Oranges derived class has method with named hasSweetToSourTaste() then the Oranges derived class is inherited with Blood_Orange dervied class that have method with named hasRedColor().

3. Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single base class.In, below diagram we have Fruit class that base classs which are inherited with Mango and Orange Sub class (derived classes).

Hierarchical Inheritance : Example

In below example, we have Fruit base class which are inherited with Mango and Orange sub class and this Mango and Orange class has two methods with named haSweetToTartTaste() and hasSweetToCreamyTaste() respectively.

4. Multiple Inheritance

In multiple inheritance, a single derived class inherits from multiple base classes. C# doesn't support multiple inheritance. However, we can achieve multiple inheritance through interfaces. In below diagram, Vegitables and Fruits are two sub classes of Food base class which have many to many relationship with each other.

Multiple Inheritance : Example

In below example, we have Food base class which are inherited with Vegetables and Fruits sub classes that have two methods with named IsVegetables() and IsFruit() respectively. AS Fruits and Vegetables have many to many relationship which can not be implemented directly so we did it with interface.

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. The combination of multilevel and hierarchical inheritance is an example of Hybrid inheritance.

Hybrid Inheritance : Example

In below example, we have Food (base class) Which has method with named forEat() that is inherited with Fruit (derived class) which has method with named isFood() that are inherited with Mango and Orange sub class and this Mango and Orange classes have two methods with named haSweetToTartTaste() and hasSweetToCreamyTaste() respectively.

34. What is method overloading in C#?

When you define multiple methods in a class with the same name but with different parameter lists. This method parameter must differ in the numbers, types, and order of parameters. It is called method overloading. Method overloading is a form of polymorphism.

35. What is method overriding in C#?

when you have a derive class (child class) and base class (parent classs). You can implement the base class (parent class) methods in derived class (child class). In base class we use virtual keyword for defining the method. We use override keyword for implementing the base class method in derive class. Overriding is feature of Object Oriented Programming .

36. What is base keyword in C#?

When you have inheritance between two or more classes. One is base classs (parent class). Another is derived class (child class). You want to access the members such as fields, properties, and methods, within a derived class. In this type of situation you can use base keyword in derived class to access members of base class.

37. What is polymorphism in C#?

Polymorphism mean many forms that we usually archived with static binding and dynamic binding. which also called late binding and early binding.

The below are two types of Polymorphism in C#:

Compile-Time Polymorphism (Static Binding):

When compiler decide which method or operator to call based on the number and types of arguments or operands, It is called compile time polymorphism. It usually occurs in from of method overloading or operator overloading.

So, the best example of compile-time polymorhism is method overloading.

Run-Time Polymorphism (Dynamic Binding):

This type of polymorphism is resolved at runtime. It occurs when you have method overriding, where you have a derive class (child class) and base class (parent classs). You can implement the base class (parent class) methods in derived class (child class).

In base class we use virtual keyword for defining the method. We use override keyword for implementing the base class method in derive classs. It is feature of Object Oriented Programming.

38. What is sealed class in C#?

When you have class as base class and you want that any other derived class can not inherit this base class then we declared the base class as sealed class. The sealed keyword also can be used with method, property or event in the derived class to prevent further overriding. This is often done to protect the integrity of a class or to prevent unintentional modifications to its behavior.

39. What is interface in C#?

Multiple inheritance is not supported in C#. So for accomplishing multiple inheritance (many to many relationship) we use interface. For making interface we use interface keyword and give a interface name. Interface have method without body. A set of method, properties signatures are defined in interface that can be implement only in that class which derived from this interface. A class which implement to interface that will have methods with body.

In below diagram, Vegitables and Fruits are two sub classes of Food base class which have many to many relationship with each other.

Rules for using Interface in C#

An interface is defined using the interface keyword.

It can contain method signatures, properties, events, and indexers, but cannot contain any implementation.

A class or struct that implements an interface must provide an implementation for all its members.

A class can implement multiple interfaces, separating them with commas.

Interfaces can inherit from other interfaces. A class implementing the derived interface must implement members of both the derived and base interfaces.

Members of an interface are implicitly public and cannot have any access modifiers.

Interfaces can define properties and events, which classes must implement.

Interfaces cannot contain fields. Only members (methods, properties, events, indexers) are allowed.

As of C# 8.0, interfaces can contain static members, but they are typically not implemented by the implementing class.

Since C# 8.0, you can use nullable reference types in interfaces to indicate whether members can be null.

40. What is namespace in C#?

A namespace is a container of group related classes, structs, enums, interfaces, and other types. You can organize related classes in a specific namespace. Namespaces help avoid naming conflicts between types and provide a hierarchical structure for organizing your code.

Some Important Features of Namespace

Syntax of Declaring Namespace

Here, in below example of integer type field

namespace - Here, namespace is keyword.

FruitUtility - FruitUtility is name of namespace.

NameSapce Syntax : Example

Copy Code

namespace FruitUtility

{

// Types and members go here

}

41. What is access modifiers in C#?

Access modifiers are keywords used to specify the declared accessibility of a member or a type. With access modifier we defined the boundary of fields, methods, constructor, properties and events. With access modifier we told a program either it's code will accessible within same class or within another classs ot it will be accessible within sname assembly or within another assembly. So we set the boundary of accessibility of code with access modifier.

Access modifiers with it's visibility and accessibility:

Public (public):

The most permissive access modifier, public allows a type or member to be accessed from anywhere inside code.

Here in below example, we have Fruit class which have fruitName public field and Color() public method which is accessible outside class.

Private (private):

The least permissive access modifier, private restricts access to the containing type only. Private members are not visible to other types or outside the current class.

Here in below example, we have Fruit class which have fruitName private field and Color() private method which is accessible inside the class only. If you will try to acces outside the class you will get below error.

Protected (protected):

Members with the protected access modifier can be accessed within the containing class and by derived classes. They are not accessible outside of the class hierarchy.

In below example, Fruit class has protected member fruitName and method Color() that are only accessible within it's own class or it's inherited class CSharpTutorial.

Internal (internal):

The internal modifier allows access within the same assembly (or module) but not from outside assemblies. Types and members marked as internal can be seen and used by code in the same assembly.

In below example, we have assembly with named FruitFirstApplication which an internal member.We have anohter assembly with named FruitSencondApplication. We have included assembly with this syntax using FruitFirstApplication in application so that we can uyse it in second assembly FruitSencondApplication. Fruit1 class is now accessible within assembly FruitSencondApplication.

Protected Internal (protected internal):

The protected internal allows access within the containing class and by derived classes that are in the same assembly.

In below example We have Fruit1 class in FruitFirstApplication assembly and Fruit1 class has protected internal field with name fruitName. This protected internal fruitName field is accessible within same assembly and within another assembly where it is dervied.

Private Protected (private protected):

The private protected allows access within the containing class and by derived classes that are in the same assembly. It was introduced in C# 7.2. It restricts access outside the assembly.

In below example, Fruit1 class and CSharpTutorial class are defined within same assembly and fruitName field of Fruit1 class are private protected which mean this field can only access within same assembly (FruitFirstApplication) and it we will try to access it from another assembly it will give us an error.

42. What is encapsulation in C#?

When you have such type of data in which you need the grouping of related operation into single unit, You can use encapsulation. Access modifier (such as Private, Public, Protected, Interval etc.) can be used for achieving the encapsulation. The encapsulation is used most commonly for data hiding.

Another real-world example of encapsulation can be your fruit backet. The backet contains different fruits like a Mango, Oranges, Apple, etc it. To get any fruit, you need to open that backet. Similarly, in C#, an encapsulation unit contains its data and behavior within it, and in order to access them, you need an object of that unit.

Another real-world example of encapsulation can be your sweet backet. The backet contains different sweets like a Rasgulla, Apple cake, Red velvet, etc it. To get any sweet, you need to open that backet. Similarly, in C#, an encapsulation unit contains its data and behavior within it, and in order to access them, you need an object of that unit.

When you have a class, method, property that doe not required implementation in current class you can make the that class, method, property abstract, Once we have defined abstract class, method and properties in base class then this abstract class, method and porperty can be overridden or implemented in derived (child) classes.

Note: The abstract class, method, property is declared with abstract keyword in the base class and has no body.

Detail of abstract classes methods and properties:

Abstract Class and Method:

We can not create the instance of abstract class directly. It serves as a blueprint for other classes (derived classes) to inherit from. Abstract classes can contain both abstract and non-abstract members. Abstract methods within an abstract class are meant to be overridden by derived classes, while non-abstract methods can have implementations and are inherited as is.

In below example, We have abstract class Fruit which have one abstract method with named FruitColor() and also have non-abstract method with named FruitTaste().

Abstract Method Implementation:

Abstract methods are declared within an abstract class and do not have a method body. They are intended to be implemented by any non-abstract derived class. When you inherit from an abstract class and override its abstract methods, you provide the specific implementation.

In below example, We have abstract class Fruit which have one abstract method with named FruitColor() and also have non-abstract method with named FruitTaste(). We have a Mango sub class also which is inherited with Fruit class. This Mango sub class has overridden the abstract method FruitColor().

Abstract Properties and Indexers:

Similar to abstract methods, you can declare abstract properties and indexers within an abstract class. These properties and indexers must be implemented in derived classes.

In below example, we have a Fruit abstract class which have abstract property with named ColorName()

Using Abstract Classes and Members:

When you create a derived class from an abstract class, you must provide concrete implementations for all abstract members. You can also choose to override non-abstract members if needed.

In below example, we have abstract class with named Fruit and this abstract class has abstract property with named ColorName. We have Mango sub class which is inherited with Fruit base class. In Mango sub class we have implemention of ColorName property.

Rules for using Abstract Class in C#

Declaration

An abstract class is declared using the abstract keyword.

Abstract Members

An abstract class can contain abstract members (methods, properties, etc.) that do not have an implementation. These must be implemented by any non-abstract derived class.

Concrete Members:

An abstract class can also contain concrete (non-abstract) members that have implementations, which can be inherited by derived classes.

Inheritance:

A derived class must implement all abstract members of the base abstract class unless it is also declared as abstract.

Instantiation:

You cannot create an instance of an abstract class directly.

Constructor:

Abstract classes can have constructors, which can be called by derived classes.

Access Modifiers:

Abstract members can have access modifiers (e.g., public, protected, etc.), controlling their visibility in derived classes.

Interface Implementation:

An abstract class can implement interfaces, allowing it to provide some base functionality while requiring derived classes to fulfill the interface contract.

Polymorphism:

Abstract classes support polymorphism. You can use a reference of an abstract class type to refer to an instance of a derived class.

45. What is partial class in C#?

When you have a class that is too large to manage, you can split it into multiple files using partial classes. A partial class is a class that is divided into multiple files, each containing a part of the class definition. All parts must use the partial keyword. The compiler combines all parts into a single class during compilation.

Rules for making Partial Classes

Definition in Separate Files:

Partial classes are defined across multiple files. Each part of the partial class must use the partial keyword.

Same Namespace, Class Name:

All parts of the partial class must belong to the same namespace and have the same class name. This ensures that the compiler can merge them into a single class.

Getting and Changing Fields:

Members (fields, properties, methods, etc.) of the partial class can be spread across the files where the class is defined. This allows developers to logically group related members together.

Accessibility Modifiers Must Match:

If a member is defined with a specific access modifier in one part of the partial class, it must have the same access modifier in all other parts.

Modifiers Can Differ:

Other modifiers like static, abstract, virtual, etc., can differ across different parts of the partial class. However, they must be compatible with each other (for example, you can't have one part of the class as static and another as non-static.

Order of Definition Doesn't Matter:

The order in which the parts of the partial class are defined doesn't matter. The compiler merges them together to create a single class.

One Part Can Inherit:

If a partial class is inheriting from a base class or implementing an interface, any part of the partial class can include the inheritance/interface implementation. However, you can't redefine the base class or interface in the other parts.

Field Initializers:

If one part of the partial class contains field initializers (initializing fields at their declaration), those initializations will be performed before the constructor body of the other parts executes.

46. What is try catch finally in C#?

The try-catch block in C# is used for exception handling. When you write a piece of code it can be crash any time due to incorrect data and can throw error but try-catch block allow us to catch these exception and display it in page. The below is syntax of try-catch block in C#.

In the example above:

try:

The try block contains the code that may throw an exception. In this case, we're trying to divide 10 by 0, which will result in a DivideByZeroException.

catch:

The catch block is used to catch and handle exceptions. In this example, we catch the base Exception class, which can catch any exception type. You can catch specific exceptions by specifying their types instead of Exception. Inside the catch block, you can log the error, perform cleanup, or take appropriate actions to handle the exception.

finally:

The finally block is optional and is used for code that should always run, regardless of whether an exception occurred or not. This is useful for cleanup tasks like closing files or releasing resources.

47. What is a Hash table class in C#?

The HashTable is related to System.Collections namespace. The HashTable contains on key-value pairs. Where each key is associated with a unique value.

48. What is the Generics in C#?

C# Generics allow us to create a single class or method, interfaces, methods, and delegates that can be used with different types of data. Generics enable you to write more flexible and reusable code by allowing you to create components that can work with any data type.

49. What is the Events in C#?

In C#, an event is a mechanism that allows a class or object to notify other classes or objects when something happens. It is essentially a way of implementing the observer design pattern.

50. What is the Delegate in C#?

In C#, a delegate is a type that represents references to methods with a specific signature. It allows methods to be treated as objects, which can be store in variables, passed as arguments to other methods, or returned from methods. Delegates are similar to function pointers in C and C++ but are type-safe.

51. What is Nullable Types in C#?

In C#, nullable types are a feature introduced to allow value types (such as int, double, bool, etc.) to have a value of null, in addition to their normal range of values. This is particularly useful in scenarios where you need to represent the absence of a value or where a value might legitimately be undefined.

52. What is Iterators in C#?

In C#, iterators provide a convenient way to iterate over collections or sequences of data. They allow you to define custom iteration behavior for your classes, similar to how you use foreach with built-in collections like arrays or lists.

53. What is a Reflection in C#?

Reflection is the process of examining and manipulating the structure and behaviour of program at runtime.With Reflection anybody can determines types, methods, properties, Constructor and other members of your program dynamically, without necessarily knowing them at compile time. Reflection provides a powerful way to interact with types and objects in a flexible manner.you can use reflection to get information about events defined within a type, such as their names, event handlers, and other metadata.

54. What is a JIT compiler?

The JIT (Just-In-Time) compiler in C# is part of the .NET runtime that converts MSIL (Microsoft Intermediate Language) code into native machine code at runtime. This allows the program to run on the target machine efficiently by compiling code just before execution.

55. What is tuple in C#?

A Tuple in C# is a data structure that allows you to store a group of values of different types in a single object. It is defined using the Tuple class or the value tuple syntax ((type1, type2, ...)). Tuples are useful for returning multiple values from a method without using out parameters or custom classes.

var person = (Name: "John", Age: 30);Console.WriteLine(person.Name); // Output: John

56. What is the difference between “is” and “as” operators in C#?

The is operator checks if an object is compatible with a specified type and returns a boolean (true or false). The as operator attempts to cast an object to a specified type and returns the object if successful; otherwise, it returns null instead of throwing an exception.

object obj = "Hello";if (obj is string) { Console.WriteLine("It's a string"); }string result = obj as string; // result = "Hello"

For more userful tutorial please click here: c# interview questions

Top comments (0)