Table of contents
1.
Introduction
2.
Basic C# Interview Questions
2.1.
1. Explain C# in just two statements.
2.2.
2. Can you write a code on Boxing and Unboxing in C#? 
2.3.
3. In C#, What is the difference between a struct and a class?
2.4.
4. What exactly is cohesion?
2.5.
5. Why are strings immutable in C#?
2.6.
6. What are the distinctions between Array and Array List in C#?
2.7.
7. What exactly are Jagged Arrays?
2.8.
8. Define constructor in the easiest language constructors.
2.9.
9. What exactly is the distinction between public, static, and void?
2.10.
10. In C#, What are partial classes?
2.11.
11. What exactly are Indexers in C#?
2.12.
12. What is the difference between Custom Control and User Control?
2.13.
13. What is the distinction between System.String and System.Text.StringBuilder?
2.14.
14. What exactly is reflection in C#?
2.15.
15. Why is the "finally" block used in C#?
3.
Advanced C# Interview Questions
3.1.
16. In C#, what is the difference between the "out" and "ref" parameters?
3.2.
17. What is the purpose of 'Data Type Conversion' in C#?
3.3.
18. What is the GAC, and where can I find it?
3.4.
19. What is a circular reference in C#?
3.5.
20. What exactly is an Object Pool in.Net? What does it imply?
3.6.
21. Explain Accessibility Modifiers in C#.
3.7.
22. What exactly are Anonymous Types in C#?
3.8.
23. What is the distinction between the methods Finalize() and Dispose()?
3.9.
24. What exactly are Custom Exceptions?
3.10.
25. What exactly are delegates?
3.11.
26. Write a program in C# to find if a positive integer is prime or not?
3.12.
27. Write a program in C# to find the substring from a given string?
3.13.
28. Write a program in C#  to reverse the order of the given words?
3.14.
29. What is Console application?
3.15.
30. Is C# code is managed or unmanaged code?
4.
Frequently Asked Questions
4.1.
What are the most important things that you must do in an interview?
4.2.
What are all the C# important topics for an experienced interview?
4.3.
What is Garbage collection in c#?
4.4.
How to prepare for C# Coding interview?
5.
Conclusion
Last Updated: Jul 29, 2024
Medium

C# interview questions for 5 years Experience

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Advanced C# Interview Questions

16. In C#, what is the difference between the "out" and "ref" parameters?

Ans: The "out" parameter can be passed to a method without being initialized, whereas the "ref" parameter must be initialized before it can be used.

17. What is the purpose of 'Data Type Conversion' in C#?

Ans: The purpose of data type conversion is to avoid situations of runtime error during data type change or conversion.

 

18. What is the GAC, and where can I find it?

Ans: The Global Assembly Cache is abbreviated as the GAC. Shared assemblies are stored in the GAC, allowing applications to share assemblies rather than having them distributed with each application. Thanks to versioning, multiple assembly versions can exist in the GAC—applications can specify version numbers in the config file. To manage the GAC, use the gacutil command line tool.

 

19. What is a circular reference in C#?

Ans: This is a situation in which multiple resources depend on each other, resulting in a lock condition and unused resources.

 

20. What exactly is an Object Pool in.Net? What does it imply?

Ans: Object Pooling in.NET allows objects to be kept in a memory pool and reused without having to recreate them. This article defines object pooling in.NET and shows how to implement it in C#.

Object Pool is a container for ready-to-use objects. Whenever a new object is requested, the pool manager accepts the request and fulfills it by allocating an object from the pool.

 

21. Explain Accessibility Modifiers in C#.

Ans: Access modifiers are keywords that specify the level of accessibility of a type member or the type itself. A public class, for example, is available to the entire world, whereas an internal class may be available only to the assembly.

 

22. What exactly are Anonymous Types in C#?

Ans: Anonymous types enable us to create new types without having to define them. This method defines read-only properties in a single object without explicitly defining each type. Type is generated by the compiler and is only available for the current block of code. The compiler also infers the type of properties.

 

23. What is the distinction between the methods Finalize() and Dispose()?

Ans: When we want an object to release unmanaged resources, we call dispose(). Finalize(), on the other hand, serves the same purpose but does not guarantee garbage collection of an object.

 

24. What exactly are Custom Exceptions?

Ans: There are times when errors must be handled in accordance with user requirements. Custom exceptions and defined exceptions are used for them.

 

25. What exactly are delegates?

Ans: Delegates are similar to function pointers in C++, except that, unlike function pointers, they are type safe. Delegates are required because they allow for creating far more generic type-safe functions.

 

26. Write a program in C# to find if a positive integer is prime or not?

Ans: 

static void Main(string[] args) 
{ 
    if (FindPrime(47)) 
    { 
        Console.WriteLine("Prime"); 
    } 
    else 
    { 
        Console.WriteLine("Not Prime"); 
    } 
    Console.ReadLine(); 
}   
internal static bool FindPrime(int number) 
{ 
    if (number == 1) return false; 
    if (number == 2) return true; 
    if (number % 2 == 0) return false; 
     var squareRoot = (int)Math.Floor(Math.Sqrt(number)); 
     for (int i = 3; i <= squareRoot; i += 2) 
    { 
        if (number % i == 0) return false; 
    } 
     return true; 
}

 

27. Write a program in C# to find the substring from a given string?

Ans:

internal static void findallsubstring(string str) 
{ 
   for (int i = 0; i < str.Length; ++i) 
   { 
       StringBuilder subString = new StringBuilder(str.Length - i); 
       for (int j = i; j < str.Length; ++j) 
       { 
           subString.Append(str[j]); 
           Console.Write(subString + " "); 
       } 
   } 
}

 

28. Write a program in C#  to reverse the order of the given words?

Ans:

internal static void ReverseWordOrder(string str) 
{ 
  int i; 
  StringBuilder reverseSentence = new StringBuilder(); 
   int Start = str.Length - 1; 
  int End = str.Length - 1; 
   while (Start > 0) 
  { 
      if (str[Start] == ' ') 
      { 
          i = Start + 1; 
          while (i <= End) 
          { 
              reverseSentence.Append(str[i]); 
              i++; 
          } 
          reverseSentence.Append(' '); 
          End = Start - 1; 
      } 
      Start--; 
  } 
   for (i = 0; i <= End; i++) 
  { 
      reverseSentence.Append(str[i]); 
  } 
  Console.WriteLine(reverseSentence.ToString()); 
}

 

29. What is Console application?

Ans: An application that may be run in Windows command prompt is referred to as a console application. Building a console application is ideally the first step for any .Net beginner to take.

 

30. Is C# code is managed or unmanaged code?

Ans: Because the Common Language Runtime can convert C# code to Intermediate language, therefore C# is considered managed code.

Frequently Asked Questions

What are the most important things that you must do in an interview?

There are a few points that you have to remember, they are:

  • Do Enough Research on the Company
  • Be On-Time for the Interview
  • Be Respectful of the interviewer
  • Good Non-Verbal Behavior.
     

What are all the C# important topics for an experienced interview?

The important topics from C#.NET are listed below:

  • CLR.
  • CLS.
  • CTS.
  • .NET Framework
  • Classes and objects
  • OOP’s
  • Difference b/w Abstract & Interface.
     

What is Garbage collection in c#?

The garbage collector (GC) is in charge of memory allocation and release. The garbage collector is a memory manager that runs in the background. You don't need to know how to allocate and release memory or how to manage the lifetimes of the things that use it.

How to prepare for C# Coding interview?

Practice coding questions on any platform, give mock interviews, go through interview experiences of other people and once you have done all this you are ready for interview.

Conclusion

In this article, we covered common C# interview questions, helping you gain a solid understanding of questions for your upcoming interviews.

We hope that this blog has helped you enhance your knowledge about the C# interview questions and if you would like to learn more, check out our articles C#C# interview questions.NET Framework, and many more on our Coding Ninjas Studio.

Explore more interview questions related article:

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass