Introduction
Well, if you have worked with C# for almost 5 years, then you must be familiar with all the basics and intermediaries of the language. And if you are looking for a job, then coding ninjas got you covered. In this blog, we are going to discuss 30 C# interview questions for 5 years experience that have a high chance of being asked in your upcoming interview.
Basic C# Interview Questions
1. Explain C# in just two statements.
Ans: C# is a general-purpose programming language that includes object-oriented, functional, and component-oriented programming. It is used to develop a wide range of programs and applications, including mobile apps, desktop apps, cloud-based services, websites, enterprise software, and games.
2. Can you write a code on Boxing and Unboxing in C#?
Ans: The methods of boxing and unboxing are used for type conversions. Boxing is converting from a value type to a reference type. Boxing is an implicit conversion. Here's an example of C# boxing.
// Boxing
int num1 = 1;
Object obj = num1;
Console.WriteLine(num1);
Console.WriteLine(obj);
Unboxing is the process of converting from a reference type to a value type. Here's a C# example of unboxing.
// Unboxing
Object obj2 = 1;
int num2 = (int)obj;
Console.WriteLine(num2);
Console.WriteLine(obj);
3. In C#, What is the difference between a struct and a class?
Ans: Class and struct are both user-defined data types, but they differ significantly:
Struct | Class |
---|---|
The struct is a value type in C# inherited from System.Value Type. | The class is a reference type in C# inherited from the System.Object Type. |
Struct is usually used for smaller amounts of data. | Classes are usually used for large amounts of data. |
Struct can’t be inherited from other types. | Classes can be inherited from other classes. |
A structure can't be abstract. | A class can be an abstract type. |
No need to create an object with a new keyword. Do not have permission to create any default constructor. | We can create a default constructor. |
4. What exactly is cohesion?
Ans: In OOPS, we write code in modules. Each module is responsible for a specific task. Cohesion measures how closely a module's responsibilities are related.
Greater cohesion is always preferable. Benefits of increased cohesion include:
- Module maintenance is improved.
- Improve reusability.
5. Why are strings immutable in C#?
Ans: String values are immutable, which means they cannot be changed once they have been created. Any change to a string value creates a new string instance, resulting in inefficient memory use and extraneous garbage collection. The System is malleable. When string values change, the Text.StringBuilder class should be used.
6. What are the distinctions between Array and Array List in C#?
Ans: Arrays store values or elements of the same data type, whereas array lists store values of various data types.
Arrays will use a fixed length, whereas array lists will not.
7. What exactly are Jagged Arrays?
Ans: In C#, an array with arrays as its elements is called a jagged array. A ragged array can have pieces of various shapes and sizes. An "array of arrays" is another name for a jagged array. In C#, a unique type of array is presented. A jagged array is a stack of arrays where each array index's length can vary.
8. Define constructor in the easiest language constructors.
Ans:A function is a class member function with the same name as the class. When an object class is created, the function is automatically called. While initialising the class, it constructs the values of data members.
9. What exactly is the distinction between public, static, and void?
Ans: The distinction between them are following:
Public: Variables and methods declared as public are accessible from anywhere in the application.
Static: Without creating an instance of the class, static declared variables or methods are globally accessible. Static members are not globally accessible by default; it depends on the type of access modification used. The compiler saves the method's address as the entry point and uses it to start execution before any objects are created.
Void: It is a type modifier that indicates that the method or variable returns null.
10. In C#, What are partial classes?
Ans: Partially implemented classes distribute the functionality of a single class across multiple files. During the compilation process, these multiple files are combined into one. The partial keyword is used to create the partial class.
public partial Class_name{
// code
}
Methods, interfaces, and structures' functionalities can be easily separated into multiple files. You can also include nested partial classes.
11. What exactly are Indexers in C#?
Ans: Smart arrays are indexers that allow access to a member variable. Indexers make array features available to member variables. They are made with the Indexer keyword. Indexers are not permanent members.
<return type> this[<parameter type> index]
{
get{
// return the value from specified index of an internal collection
}
set{
// set values at specified index in an internal collection
}
}
12. What is the difference between Custom Control and User Control?
Ans: Custom Controls are compiled code (DLL) controls that are easier to use and can be added to the toolbox. Developers can add controls to their web forms by dragging and dropping them. Attributes can be added during the design process. Custom controls can be easily added to multiple applications (If Shared Dlls). So, if they are private, we can copy the dll to the web application's bin directory, add a reference, and use them.
User Controls are similar to ASP, including files in that they are simple to create. User controls cannot be dragged and dropped into the toolbox. They have their own design and code. Ascx is the file extension for user controls.
13. What is the distinction between System.String and System.Text.StringBuilder?
Ans: Systems.Strings are unchangeable. When we change the value of a string variable, we allocate new memory to the new value and release the previous memory allocation. System.Text.StringBuilder was created with the concept of a mutable string in mind, allowing a variety of operations to be performed without the need for a separate memory location for the modified string.
14. What exactly is reflection in C#?
Ans: During runtime, C# reflection extracts metadata from data types.
To implement reflection in the .Net framework, simply use the System.Reflection namespace in your program to retrieve the type, which can be any of the following:
- Assembly
- ConstructorInfo
- MemberInfo
- ParameterInfo
- FieldInfo
- EventInfo
- PropertyInfo
15. Why is the "finally" block used in C#?
Ans: The "Finally" block will be executed regardless of the exception. When an exception occurs while executing the code in the try block, control is returned to the catch block, and the "finally" block is executed. As a result, closing the database connection and releasing the file handlers can be kept in the "finally" block.