C# Dot Net Interview Preparation Part 8 | Top 6 Frequently Asked Interview Questions and Answers
C# Dot Net Interview Preparation Part 8

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:

  • == (Equality Operator): Compares reference equality for reference types and value equality for value types.
  • .Equals(): Compares the contents of an object (overridable for custom behavior).

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:

  • const: Value is assigned at compile-time and cannot change.
  • readonly: Value can be assigned in the constructor but remains constant afterward.
  • static: Belongs to the class rather than an instance and is shared among all instances.

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:

  • String: Immutable (creates a new instance for every modification).
  • StringBuilder: Mutable (efficient for multiple modifications).

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:

  • Boxing: Converting a value type (int, double) to a reference type (object).
  • Unboxing: Converting a reference type back to a value type.

Code Example:

int num = 10;
object obj = num;  // Boxing

int unboxedNum = (int)obj;  // Unboxing
        

Hashtags for LinkedIn/YouTube Video:

  • #DotNetInterviewPrep
  • #CSharpBasics
  • #LearnDotNet
  • #ProgrammingForBeginners
  • #DotNetDevelopment
  • #CodingTips
  • #SoftwareEngineer
  • #InterviewQuestions
  • #AspNetCore
  • #DotNetLearning

To view or add a comment, sign in

Others also viewed

Explore topics