TOP 20 C# QUESTIONS FOR CRACKING YOUR INTERVIEW

 

 

C# programming is said to be easier than other programming languages.
More development options: You can use C# to create web applications or gaming apps. Automatic garbage collection, interfaces, and other features in C# help developers create better applications.

 

A larger target market: Collaboration with Microsoft gives C#-based applications an advantage because they can reach a wider audience.

 

But since C# is such a popular programming language, many large and small businesses use it to build their products. So, to ace, the interviews, prepare yourself with basic and advanced level C# questions.

 

 

 

1. What makes managed code different from unmanaged code?

 

The.NET framework’s managed code allows you to run your code in a managed CLR runtime environment.

Managed code is executed by the managed runtime environment rather than the operating system.

Provides garbage collection, exception handling, and other features.

 

Unmanaged code is defined as code that does not run on the CLR and operates outside of the.NET framework.

They operate without high-level language services because they do not provide them. C++ is a good example of this.

 


 

2. What makes an abstract class different from an interface?

 

Let’s look at the differences between an abstract class and an interface in more detail:


Because abstract classes cannot be instantiated, they cannot produce objects. The interface is similar to an abstract class because all of the methods within it are abstract methods. Surprisingly, abstract classes can have both abstract and non-abstract methods, whereas the methods of an interface are all abstract. Because abstract classes can have both abstract and non-abstract methods, we must use the Abstract keyword to declare abstract methods. In the interface, however, there is no such requirement.


Constructors are not present in an interface, but they are present in an abstract class.

				
					Abstract class:
public abstract class Shape{
public abstract void draw();
}
Interface:
public interface Paintable{
void paint();

				
			

 

 

3. What are the differences between ref and out keywords?

 

C# ref keywords pass arguments by reference and not value. To use the ‘ref’ keyword, you need to explicitly mention ‘ref’. 

				
					void Method(ref int refArgument)
{
   refArgument = refArgument + 10;
}
int number = 1;
Method(ref number);
Console.WriteLine(number);
// Output: 11

				
			

Within methods and functions, the C# out keywords pass parameters.

The keyword ‘out’ is used to pass arguments as a reference to return multiple values in a procedure. Even though it is identical to the ref keyword, the ref keyword must be initialised before it may be provided. When we want to return a result from the same variables that were supplied as an argument, the out and ref keywords come in handy.

				
					public static string GetNextFeature(ref int id)  
{  
   string returnText = "Next-" + id.ToString();  
   id += 1;  
   return returnText;  
}  
public static string GetNextFeature(out int id)  
{  
   id = 1;  
   string returnText = "Next-" + id.ToString();  
   return returnText;  
} 

				
			

 

 

4. What is the difference between an Array and ArrayList in C#?

An array is a collection of related variables grouped under a single identifier. ArrayList, on the other hand, is a collection of objects that may be individually indexed. You can use ArrayList to access a variety of functionalities, including dynamic memory allocation, item addition, searching, and sorting.

  • The size of the objects in an array is fixed when it is declared, hence the memory allocation is fixed. However, with ArrayList, it can be dynamically increased or lowered.

  • The system.array namespace contains Array, while the system.collection namespace contains ArrayList.

  • An array’s items are all of the same data type, whereas an ArrayList’s items might be of multiple data types.

  • While null values are not allowed in arrays, they are allowed in ArrayList.

 

For ex.:

				
					// C# program to illustrate the ArrayList
using System;
using System.Collections;
 
class IB {
 
   // Main Method
   public static void Main(string[] args)
   {
 
       // Create a list of strings
       ArrayList al = new ArrayList();
       al.Add("Bruno");
       al.Add("Husky");
       al.Add(10);
       al.Add(10.10);
 
       // Iterate list element using foreach loop
       foreach(var names in al)
       {
           Console.WriteLine(names);
       }
   }
}


// C# program to illustrate
// multiple class inheritance
using System;
using System.Collections;

// Parent class 1
class Scaler {

  // Providing the implementation
  // of features() method
  public void features()
  {

      // Creating ArrayList
      ArrayList My_features= new ArrayList();

      // Adding elements in the
      // My_features ArrayList
      My_features.Add("Abstraction");
      My_features.Add("Encapsulation");
      My_features.Add("Inheritance");

      Console.WriteLine("Features provided by OOPS:");
      foreach(var elements in My_features)
      {
          Console.WriteLine(elements);
      }
  }
}

// Parent class 2
class Scaler2 :Scaler{

  // Providing the implementation
  // of courses() method
  public void languages()
  {

      // Creating ArrayList
      ArrayList My_features = new ArrayList();

      // Adding elements in the
      // My_features ArrayList
      My_features.Add("C++");
      My_features.Add("C#");
      My_features.Add("JScript");
     

      Console.WriteLine("\nLanguages that use OOPS concepts:");
      foreach(var elements in My_features)
      {
          Console.WriteLine(elements);
      }
  }
}

// Child class
class ScalertoScaler : Scaler2 {
}

public class Scaler1 {

  // Main method
  static public void Main()
  {

      // Creating object of ScalertoScaler class
      ScalertoScaler obj = new ScalertoScaler();
      obj.features();
      obj.languages();
  }
}

				
			

 

 

5. In C#, how does serialisation work?

In C#, serialisation is the process of converting an object into a stream of bytes that can be stored in memory, a database, or a file. This allows the developer to save the state of an object for future reference.

 

A type’s SerializableAttribute> attribute indicates whether or not instances of that type can be serialised. All public and private fields defined as such are serialised by default.

 

 

6. What’s the difference between String and StringBuilder in C#?

A string object is immutable, meaning it can’t be changed once it’s been created. Any operation that attempts to modify the string object will result in the creation of a new string object. A string builder object, on the other hand, is malleable and can be customised as needed.

 

 

7. In C#, what is the purpose of reflection?

C# uses reflection to get type metadata at runtime. To put it another way, it gives developers access to information about loaded assemblies and the types they contain.

It is implemented using a two-step procedure. To begin, you’ll need to get the type object. Second, you can use the type to browse members such as methods and properties.

 

 

8. How would you explain object-oriented programming’s four fundamental concepts?

The following are the four fundamental concepts of object-oriented programming:

Encapsulation is the process of grouping together data and the methods that operate on it into a single, private unit.

Polymorphism refers to a type’s ability to take on multiple forms using a single interface.

Abstraction is the process of hiding unimportant programme details so that the user only sees the most important features.

Inheritance is the process by which one class derives (or inherits) its attributes and methods from another 

 

 

9. In C#, what is the difference between late and early binding?

The following are the main differences between early and late binding:

  • Early binding occurs during the compilation process, whereas late binding occurs during the execution process.

 

  • Early binding resolves method calls using class information, whereas late binding resolves method calls using the object.

 

  • Because late binding occurs at runtime, its performance is typically slower than early binding.

 

 

10. What is the difference between the C# keywords Const and ReadOnly?

In C#, the keywords Const and ReadOnly differ in several ways. Among them are:

  • ReadOnly is a runtime constant, whereas Const is a compile-time constant.

  • Const values cannot be changed, whereas ReadOnly values can.

  • Const cannot be declared within a method, but ReadOnly can.

 

 

11. What does dependency injection mean in C#?

Dependency injection (DI) is a design pattern in C# that is used to create loosely coupled code. This procedure separates the creation and binding of dependent objects from the class on which they rely. The main goal here is to make future code changes more manageable.

 

 

12. How do you fix circular references in C#?

Circular references are frequently resolved in C# by garbage collection. Circular references are systematically detected and collected by the garbage collector. Callback methods, event handlers, and dependency injection are some other solutions for circular reference issues.

 

 

13. In C#, how can a class be made inheritable without overriding the method?

It will not be overridden if the method is not virtual. If the class is inheriting from a base class that contains a virtual member function, the sealed> modifier can be used to avoid further overriding that function.

 

 

 

14. In C#, what are properties?

In C#, properties are public members of a class that allow you to access private members of the class. By making variables private, the basic principle of encapsulation allows you to hide some sensitive properties from users. In a class, the private members are not accessible otherwise. As a result, you can easily access and set the values of private members in C# by using properties.

Get and set methods, also known as accessors, make it simple to assign values. The set method assigns the value to the variables, whereas the get method extracts it.

 
 

 

15. In C#, what are indexers?

Smart arrays are indexers that allow access to a member variable. Indexers allow member variables to use array features. The Indexer keyword is used to create them. Members who are indexers are not permanent.

For instance, The indexer is defined in the same way as before.

				
					<return type> this[<parameter type> index]
{
   get{
       // return the value from the specified index of an internal collection
   }
   set{
       // set values at the specified index in an internal collection
   }
}

				
			

 

 

16. Write a program in C# Sharp to reverse a string?

				
					internal static void ReverseString(string str) 
{ 
   
  char[] charArray = str.ToCharArray(); 
  for (int i = 0, j = str.Length - 1; i < j; i++, j--) 
  { 
      charArray[i] = str[j]; 
      charArray[j] = str[i]; 
  } 
  string reversedstring = new string(charArray); 
  Console.WriteLine(reversedstring);

				
			

 

 

17. In C#, how do you distinguish between String and StringBuilder?


Strings are immutable, whereas StringBuilder creates a mutable string of characters. Instead of creating a new object, StringBuilder will make changes to the existing one.

 

StringBuilder makes changing an existing string object a lot easier. It’s more expensive to create a new object every time we need to make a change because the String class is immutable. As a result, the StringBuilder class, which can be invoked using the System, enters the picture. Namespace for text data.

 

Use String class or StringBuilder instead if a string object will not change throughout the programme.

 

 

 

18. Write a C# Sharp programme that reverses the order of the given words.
internal void of static WordOrderInReverse (string str)

 

				
					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()); 
}


				
			

 

 

19. What is a Delegate?


Delegate is a variable that stores a method reference. It is a reference type function pointer. The System is the source of all Delegates. Namespace delegation The signature of the Delegate and the method it refers to can be the same. Let’s look at an example.

				
					
public Delegate int myDel(int number); //declaring a delegate
public class Program
{
public int SubstractNumbers(int a) //Class Program has the method same signature as delegate called 
{
int difference = a - 10;
return difference;
}
public void start()
{
myDel DelegateExample = SubstractNumbers;
}
}


				
			

 

 

20. What’s the difference between System.String and System.Text.StringBuilder?

 

System.Strings are unchangeable. When you change the value of a string variable, new memory is allocated for the new value, and the old memory is released. System. Text. StringBuilder is mutable, which means it can be used to perform a variety of operations.

without allocating a separate memory location for the string that has already been modified

This is the end of our topic. We hope we helped you in some way. The topics covered in this Core  C#  Interview Questions blog are among the most sought-after skillsets for a  C# professional, according to recruiters. These C# Interview Questions will undoubtedly assist you in acing your job interview.


Best wishes for your interview!


If you still have any queries, please let us know in the comment section.