Skip to content

Instantly share code, notes, and snippets.

@erasif99
Created January 8, 2025 09:59
Show Gist options
  • Select an option

  • Save erasif99/91191f41bf91fd74bfedb830b10f36f9 to your computer and use it in GitHub Desktop.

Select an option

Save erasif99/91191f41bf91fd74bfedb830b10f36f9 to your computer and use it in GitHub Desktop.
C# Interview Question & Answer 2025
  1. What is boxing and unboxing in C#? Answer: Boxing is the process of converting a value type (e.g., int, float) to a reference type (e.g., object) so that it can be stored on the heap. Unboxing is the process of converting a reference type (e.g., object) back to a value type. It involves extracting the value from the boxed object.

  2. What is Managed or Unmanaged Code? Managed code in C# runs within a managed environment like the Common Language Runtime (CLR), providing automatic memory management and security features. Unmanaged code, however, operates outside of such environments, directly interacting with system resources and requiring manual memory management.

  3. What is the difference between a struct and a class in C#? Class and struct are both user-defined data types but have some major differences: Struct The struct is a value type in C# and inherits from System.Value Type. Struct is usually used for smaller amounts of data. Struct can’t be inherited from other types. A structure can't be abstract. No need to create an object with a new keyword. Do not have permission to create any default constructor. Class The class is a reference type in C#, and it inherits from the System.Object Type. Classes are usually used for large amounts of data. Classes can be inherited from other classes. A class can be an abstract type. We can create a default constructor.

  4. What is the difference between a value type and a reference type in C#? A value type stores its data directly, while a reference type stores a reference (memory address) to the location where its data is stored. Value types are typically primitive types like int, float, bool, etc., while reference types are usually classes, interfaces, delegates, or arrays. Value types are stored on the stack, while reference types are stored on the heap.

  5. What is the purpose of the using statement in C#? The 'using' statement is used to ensure that the resources like file streams, database connections, etc., are properly disposed of when they are no longer needed. It automatically calls the Dispose method of IDisposable interface when the block of code is exited.

  6. What is the difference between ref and out parameters in C#? The 'ref' keyword is used to pass arguments to a method by reference, meaning any changes to the parameter inside the method will reflect in the calling code. The 'out' keyword is similar to 'ref', but it is used when a method returns multiple values. It indicates that the parameter is being used to return a value from the method.

  7. What is an enum in C#? In C#, an enum (enumeration) is a value type that allows you to define a set of named constants. It provides a way to associate meaningful names with a set of related integral values, making the code more readable and maintainable. Enums are often used to represent a fixed set of possible values for a variable or parameter.

  8. What is the difference between “continue” and “break” statements in C#? 'continue' is used to skip the rest of the loop's body for the current iteration and proceed to the next iteration. It allows the loop to continue with the next iteration, ignoring the remaining code in the loop's body for the current iteration 'break'is used to terminate the loop prematurely and exit its execution. It immediately exits the loop, bypassing any remaining iterations, and continues with the code outside the loop.

  9. What is the async and await keywords used for in C#? 'async' is used to define a method that can perform asynchronous operations. 'await' is used inside an 'async' method to indicate that the method should asynchronously wait for the completion of the awaited task before proceeding.

@erasif99
Copy link
Author

erasif99 commented Jan 8, 2025

Find more question & answers at C# Interview Questions 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment