C-Sharp Interview Questions and Answers

Basic Concepts

1. What are the key features of C#?

Answer:

  • Object-oriented programming language.
  • Strongly typed language with type safety.
  • Automatic garbage collection.
  • Rich library support via .NET.
  • Supports asynchronous programming with async and await

2. What is the difference between struct and class?

Answer:

  • struct is a value type; class is a reference type.
  • struct is stored in the stack, while class is stored in the heap.
  • struct does not support inheritance, but class

 

3. What is the purpose of the ref and out keywords?

Answer:

  • ref passes a variable by reference and requires it to be initialized before being passed.
  • out passes a variable by reference but doesn’t require initialization before being passed.

4. What is the difference between const, readonly, and static?

Answer:

  • const: Value is constant and determined at compile time.
  • readonly: Value is set at runtime and can only be assigned in the constructor.
  • static: Belongs to the class rather than instances.

5. What is a nullable type in C#?

Answer:

  • Nullable types allow variables to hold null in addition to their normal values.
  • Example: int? nullableInt = null;

Object-Oriented Programming

6. Explain the principles of OOP.

Answer:

  • Encapsulation: Hiding implementation details.
  • Abstraction: Highlighting essential features while hiding unnecessary details.
  • Inheritance: Acquiring properties and behaviors from a base class.
  • Polymorphism: Ability to perform actions differently based on context (method overloading and overriding).

7. How does polymorphism work in C#?

Answer:

  • Achieved using method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).
  • Example: public class BaseClass {      public virtual void Show() {        Console.WriteLine(“Base Class”);      }    public class DerivedClass : BaseClass {     public override void Show() {          Console.WriteLine(“Derived Class”);      }

8. Can C# support multiple inheritance?

Answer:

  • No, C# does not support multiple inheritance directly through classes to avoid ambiguity. However, it supports multiple inheritance via interfaces.

9. What is an abstract class? How is it different from an interface?

Answer:

  • An abstract class can have both abstract and concrete methods.
  • An interface only contains method declarations (until default implementations were introduced in C# 8.0).

10. What is the difference between virtual, override, and new keywords in method overriding?

Answer:

  • virtual: Marks a method as overridable.
  • override: Used to override a virtual method in a derived class.
  • new: Hides a base class method without overriding.

Advanced Features

11. What is LINQ, and why is it used?

Answer:

  • LINQ (Language Integrated Query) is used to query data in a type-safe and efficient manner.
  • Example: var numbers = new[] {1, 2, 3, 4, 5};var evenNumbers = numbers.Where(n => n % 2 == 0);
  •  

12. What is the difference between IEnumerable and IQueryable?

Answer:

  • IEnumerable is used for in-memory querying.
  • IQueryable supports out-of-memory querying (e.g., querying a database).

13. What is a delegate? How is it different from an event?

Answer:

  • A delegate is a type-safe function pointer.
  • An event is a wrapper around a delegate, ensuring only subscribed methods are invoked.

14. What are generics in C#? Why are they used?

Answer:

  • Generics allow you to define type-safe classes, methods, and collections.
  • Example:

      public class GenericClass<T> { public T Data { get; set; } }

15. What is the difference between Array and ArrayList?

Answer:

  • Array: Fixed size, type-safe.
  • ArrayList: Dynamically resizable, not type-safe

16. What is the purpose of the yield keyword in C#?

Answer:

  • Used to produce an iterator block, enabling deferred execution.
  • Example:

       §  public IEnumerable<int> GetNumbers() {§      yield return 1;§      yield return 2;§      yield return 3;§  }

17. How does memory management work in C#?

Answer:

  • Managed by the .NET runtime.
  • Garbage Collector (GC) automatically deallocates unused objects

18. What is the difference between Dispose and Finalize?

Answer:

  • Dispose is used for manual cleanup (IDisposable interface).
  • Finalize is used by the Garbage Collector for cleanup.

19. What are Weak References in C#?

Answer:

  • Allow the GC to collect objects if no strong references exist.

20. What is the purpose of the GC.Collect() method?

Answer:

  • Forces immediate garbage collection, though generally not recommended.

21. Explain async and await in C#.

Answer:

  • async marks a method as asynchronous.
  • await pauses the execution until the awaited task completes.

22. What is the difference between Task and Thread?

Answer:

  • Task represents an asynchronous operation.
  • Thread is a physical thread managed by the OS.

23. What is thread safety? How can it be achieved?

Answer:

  • Thread safety ensures consistent behavior in a multi-threaded environment.
  • Achieved using locking mechanisms, lock keyword, or concurrent collections.

24. What is the ThreadPool in C#?

Answer:

  • A managed pool of threads for executing asynchronous tasks efficiently.

25. What is the Common Language Runtime (CLR)?

Answer:

  • Executes .NET programs.
  • Provides services like garbage collection, security, and exception handling.

26. What are assemblies?

Answer:

  • Compiled code libraries in .NET.
  • Two types: private and shared.

27. What is the Global Assembly Cache (GAC)?

Answer:

  • A storage for shared assemblies.
  • Used for versioning and sharing across applications.

28. What is the difference between .NET Core and .NET Framework?

Answer:

  • .NET Core: Cross-platform, open-source.
  • .NET Framework: Windows-specific, mature library support.

29. How is exception handling implemented in C#?

Answer:

  • Using try, catch, and finally
  • Example:

§  try {§  int result = 10 / 0;§  }
catch (DivideByZeroException ex)
{
§  Console.WriteLine(“Cannot divide by zero.”);§  }
finally {
§ Console.WriteLine(“Operation complete.”);§  }

30. What is the difference between throw and throw ex?

Answer:

  • throw preserves the original exception stack trace.
  • throw ex resets the stack trace.

Conclusion

This complete guide to C# interview questions and solutions presents a solid foundation for developers getting geared up for technical interviews. Covering critical subjects like fundamental standards, object-orientated programming, advanced features, memory management, threading, and.NET framework specifics, it equips applicants with the understand-a way to tackle a tremendous form of questions. The inclusion of examples and smooth explanations guarantees clarity and realistic understanding.

For maximum great instruction, interest in know-how key standards, running towards coding examples, and exploring real-international packages of the ideas discussed. Mastering the ones topics will enhance self-assurance and readiness for C# interviews.

How Can Salesforce Help Manage Remote Teams Effectively?
Discover how Salesforce CRM Services can transform your remote team management.
Salesforce CRM Services