C# Dot Net Interview Preparation Part 12 | Top 6 Frequently Asked Interview Questions and Answers
Asharib Kamal
Sr. Full Stack Developer | Specializing in .NET Technologies | C# | Dot NET Core | Asp.NET MVC | Angular | SQL | Content Creator | Transforming Ideas into High-Impact Web Solutions | 7K + Followers
Question 67: What is the difference between abstract class and interface in C#?
Answer:
Code Example:
abstract class Animal
{
public abstract void MakeSound(); // Abstract method
public void Sleep() => Console.WriteLine("Sleeping...");
}
interface IBird
{
void Fly(); // No implementation (before C# 8)
}
class Dog : Animal
{
public override void MakeSound() => Console.WriteLine("Bark!");
}
class Sparrow : IBird
{
public void Fly() => Console.WriteLine("Flying high!");
}
Question 68: What is a delegate in C#?
Answer: "A delegate is a type that holds a reference to methods with a specific signature."
Code Example:
delegate void MyDelegate(string message);
class Program
{
static void ShowMessage(string msg) => Console.WriteLine(msg);
static void Main()
{
MyDelegate del = ShowMessage;
del("Hello, Delegates!");
}
}
Question 69: What is the difference between Stack and Queue in C#?
Answer:
Feature Stack<T> Queue<T> Order LIFO (Last In, First Out) FIFO (First In, First Out) Methods Push(), Pop() Enqueue(), Dequeue()
Code Example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
Console.WriteLine(stack.Pop()); // Output: 2
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
Console.WriteLine(queue.Dequeue()); // Output: 1
}
}
领英推荐
Question 70: What is async and await in C#?
Answer: "async and await are used to write asynchronous (non-blocking) code, improving performance."
Code Example:
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
await PrintMessage();
Console.WriteLine("Finished!");
}
static async Task PrintMessage()
{
await Task.Delay(2000); // Simulate delay
Console.WriteLine("Hello, Async!");
}
}
Question 71: What is the difference between Equals() and == in C#?
Answer:
Code Example:
class Program
{
static void Main()
{
string str1 = "Hello";
string str2 = "Hello";
Console.WriteLine(str1 == str2); // True (compares values)
Console.WriteLine(str1.Equals(str2)); // True (compares values for strings)
}
}
Question 72: What is LINQ in C#?
Answer: "LINQ (Language Integrated Query) is used to query collections and databases in a readable way."
Code Example:
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
Console.WriteLine(num); // Output: 2, 4
}
}
Hashtags for LinkedIn/YouTube Video: