C# Dot Net Interview Preparation Part 14| Top 6 Frequently Asked Interview Questions and Answers
Question 79: What is the difference between == and .Equals() in C#?
Answer:
Code Example:
string a = "hello";
string b = new string("hello".ToCharArray());
Console.WriteLine(a == b); // True (compares values for strings)
Console.WriteLine(a.Equals(b)); // True (compares values)
Question 80: What is boxing and unboxing in C#?
Answer:
Code Example:
int number = 42; // Value type
object boxed = number; // Boxing
int unboxed = (int)boxed; // Unboxing
Console.WriteLine(unboxed); // Output: 42
Question 81: What is the params keyword in C#?
Answer: It allows passing a variable number of arguments to a method.
Code Example:
void PrintNumbers(params int[] numbers)
{
foreach (int number in numbers)
Console.WriteLine(number);
}
PrintNumbers(1, 2, 3, 4); // Output: 1 2 3 4
Question 82: What is the difference between const and readonly in C#?
Answer:
Code Example:
class Example
{
public const int MyConst = 10;
public readonly int MyReadonly;
public Example(int value)
{
MyReadonly = value;
}
}
Question 83: What is static class in C#?
Answer: A static class cannot be instantiated, and all its members must be static.
Code Example:
static class MathHelper
{
public static int Add(int a, int b) => a + b;
}
int sum = MathHelper.Add(3, 5); // Output: 8
Question 84: What is the difference between throw and throw ex in exception handling?
Answer:
Code Example:
try
{
throw new Exception("Something went wrong");
}
catch (Exception ex)
{
throw; // Good practice
// throw ex; // Avoid this
}
✅ Hashtags: