C# Dot Net Interview Preparation Part 8 | Top 6 Frequently Asked Interview Questions and Answers
Question 43: What is the difference between == and .Equals() in C#?
Answer:
Code Example:
string str1 = "Hello";
string str2 = "Hello";
Console.WriteLine(str1 == str2); // True (Value Comparison)
Console.WriteLine(str1.Equals(str2)); // True (Content Comparison)
object obj1 = new object();
object obj2 = new object();
Console.WriteLine(obj1 == obj2); // False (Different References)
Console.WriteLine(obj1.Equals(obj2)); // False (Default Behavior)
Question 44: What is the difference between const, readonly, and static in C#?
Answer:
Code Example:
class Example
{
public const double Pi = 3.14; // Compile-time constant
public readonly int Age; // Can be set only in constructor
public static string AppName = "MyApp"; // Shared by all instances
public Example(int age)
{
Age = age; // Can only be set here
}
}
Question 45: What is the difference between String and StringBuilder?
Answer:
Code Example:
string str = "Hello";
str += " World"; // Creates a new string object
StringBuilder sb = new StringBuilder("Hello");
sb.Append(" World"); // Modifies the same object
Question 46: What is a nullable type in C#?
Answer: "A nullable type allows value types (int, double, etc.) to hold null values."
Code Example:
int? number = null; // Nullable int
if (number.HasValue)
Console.WriteLine(number.Value);
else
Console.WriteLine("Value is null");
Question 47: What is an interface in C#?
Answer: "An interface defines a contract that implementing classes must follow, ensuring method consistency across different classes."
Code Example:
interface IAnimal
{
void MakeSound();
}
class Dog : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Bark");
}
}
Question 48: What is boxing and unboxing in C#?
Answer:
Code Example:
int num = 10;
object obj = num; // Boxing
int unboxedNum = (int)obj; // Unboxing
Hashtags for LinkedIn/YouTube Video: