C# Dot Net Interview Preparation Part 13| Top 6 Frequently Asked Interview Questions and Answers
Question 73: What is the difference between var, dynamic, and object in C#?
Answer:
var Determined at compile-time Compile-time var x = "Hello";
dynamic Determined at runtime Runtime dynamic x = 10; x = "Hello";
object Requires explicit casting Runtime object obj = 10; int num = (int)obj;
Code Example:
class Program
{
static void Main()
{
var myVar = "Hello"; // Type inferred as string
dynamic myDynamic = 5;
myDynamic = "Now it's a string"; // Allowed
object myObject = 10;
int num = (int)myObject; // Explicit casting required
}
}
Question 74: What is NullReferenceException and how to handle it?
Answer: "NullReferenceException occurs when trying to access an object that is null." Use null checks or null-conditional (?.) operators to prevent it.
Code Example:
class Program
{
static void Main()
{
string text = null;
// Bad practice: Causes NullReferenceException
// Console.WriteLine(text.Length);
// Good practice: Check for null
if (text != null)
Console.WriteLine(text.Length);
// Using null-conditional operator
Console.WriteLine(text?.Length);
}
}
Question 75: What is out, ref, and in parameters in C#?
Answer:
Keyword Used For Value Initialization Required? Example Usage out Returning multiple values No (must be assigned inside method) Method(out x); ref Modifying existing variable Yes (must be initialized before method call) Method(ref x); in Read-only parameters Yes (cannot be modified inside method) Method(in x);
Code Example:
class Program
{
static void SetValues(out int a)
{
a = 10; // Must be assigned inside method
}
static void ModifyValue(ref int b)
{
b += 10; // Can modify the existing value
}
static void ReadOnlyMethod(in int c)
{
// c = 20; // Error: Cannot modify in parameter
Console.WriteLine(c);
}
static void Main()
{
int x;
SetValues(out x);
Console.WriteLine(x); // Output: 10
int y = 5;
ModifyValue(ref y);
Console.WriteLine(y); // Output: 15
int z = 30;
ReadOnlyMethod(in z);
}
}
Recommended by LinkedIn
Question 76: What is a Tuple in C#?
Answer: "A Tuple is a lightweight data structure that groups multiple values."
Code Example:
using System;
class Program
{
static (int, string, bool) GetPerson()
{
return (1, "John Doe", true);
}
static void Main()
{
var person = GetPerson();
Console.WriteLine($"ID: {person.Item1}, Name: {person.Item2}, Active: {person.Item3}");
}
}
Question 77: What is Indexers in C#?
Answer: "Indexers allow a class to be accessed like an array using this keyword."
Code Example:
class MyCollection
{
private string[] data = new string[3];
public string this[int index]
{
get { return data[index]; }
set { data[index] = value; }
}
}
class Program
{
static void Main()
{
MyCollection collection = new MyCollection();
collection[0] = "Hello";
collection[1] = "World";
Console.WriteLine(collection[0]); // Output: Hello
}
}
Question 78: What is the difference between override, new, and virtual in C#?
Answer:
Keyword Used In Purpose virtual Base class Allows method overriding in derived classes override Derived class Replaces base class implementation new Derived class Hides base class method (without overriding)
Code Example:
class BaseClass
{
public virtual void Show() => Console.WriteLine("Base Virtual Method");
public void Display() => Console.WriteLine("Base Method");
public virtual void Hidden() => Console.WriteLine("Base Hidden Method");
}
class DerivedClass : BaseClass
{
public override void Show() => Console.WriteLine("Derived Overridden Method");
public new void Display() => Console.WriteLine("Derived New Method");
}
class Program
{
static void Main()
{
BaseClass obj1 = new DerivedClass();
obj1.Show(); // Output: Derived Overridden Method
obj1.Display(); // Output: Base Method
DerivedClass obj2 = new DerivedClass();
obj2.Display(); // Output: Derived New Method
}
}
Hashtags for LinkedIn/YouTube Video: