C# DotNet Interview Preparation - Part 10
C# DotNet Interview Questions and Answers - Part 10

C# DotNet Interview Preparation - Part 10


Question 55: What is the difference between ref and out parameters in C#?

Answer: Both ref and out are used to pass arguments by reference, but:

  • ref: The variable must be initialized before passing.
  • out: The variable does not need to be initialized before passing.

Code Example:

class Program
{
    static void ModifyRef(ref int number)
    {
        number += 10;
    }

    static void ModifyOut(out int number)
    {
        number = 20; // Must be assigned before use
    }

    static void Main()
    {
        int a = 5;
        ModifyRef(ref a);
        Console.WriteLine(a); // Output: 15

        int b;
        ModifyOut(out b);
        Console.WriteLine(b); // Output: 20
    }
}
        

Question 56: What is var, dynamic, and object in C#?

Answer:

  • var: Statically typed; type determined at compile-time.
  • dynamic: Dynamically typed; type determined at runtime.
  • object: Base type for all types; requires type casting.

Code Example:

var name = "John"; // Inferred as string
dynamic age = 30; // Can change type at runtime
object obj = "Hello"; // Needs explicit casting

age = "Thirty"; // Allowed because 'dynamic' can change type
        

Question 57: What is a sealed class in C#?

Answer: "A sealed class cannot be inherited by another class, ensuring security and preventing modifications."

Code Example:

sealed class SealedClass
{
    public void Show() => Console.WriteLine("This is a sealed class.");
}

// class Derived : SealedClass {}  // Error: Cannot inherit from sealed class
        

Question 58: What is an Indexer in C#?

Answer: "An indexer allows objects to be accessed like an array using [] notation."

Code Example:

class SampleCollection
{
    private string[] data = new string[5];

    public string this[int index]
    {
        get { return data[index]; }
        set { data[index] = value; }
    }
}

class Program
{
    static void Main()
    {
        SampleCollection collection = new SampleCollection();
        collection[0] = "Hello";
        Console.WriteLine(collection[0]); // Output: Hello
    }
}
        

Question 59: What is the difference between IEnumerable and IQueryable in C#?

Answer:

Feature IEnumerable IQueryable Execution In-memory Database (SQL) Performance Slow for large data Optimized for queries Lazy Loading No Yes

Code Example:

IEnumerable<int> numbers = new List<int> { 1, 2, 3 }.Where(n => n > 1);
IQueryable<int> dbNumbers = dbContext.Numbers.Where(n => n > 1);
        

Question 60: What is a Tuple in C#?

Answer: "A Tuple is a lightweight data structure used to store multiple values in a single variable."

Code Example:

var person = ("John", 30, "Developer");
Console.WriteLine($"{person.Item1} is a {person.Item3}"); // Output: John is a Developer

// Named Tuples
var personNamed = (Name: "Alice", Age: 25);
Console.WriteLine($"{personNamed.Name} is {personNamed.Age} years old.");
        

Hashtags for LinkedIn/YouTube Video:

  • #DotNetInterviewQuestions
  • #CSharpTips
  • #LearnCSharp
  • #SoftwareEngineering
  • #DotNetCoreTutorial
  • #ProgrammingBasics
  • #TechLearning
  • #CodingForBeginners
  • #CSharpConcepts
  • #AspNetCoreDevelopment



Gayathri Arpula

B. Tech Graduate | Anurag College Of Engineering

3mo

Very helpful

To view or add a comment, sign in

More articles by Asharib Kamal

Insights from the community

Others also viewed

Explore topics