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

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

?

Question 25: What is Dependency Injection (DI) in .NET?

Answer:

"Dependency Injection (DI) is a design pattern used to achieve Inversion of Control (IoC) between classes and their dependencies. In .NET, DI is commonly used to manage dependencies like services, repositories, and configurations."

Example:

public interface ILogger
{
    void Log(string message);
}

public class ConsoleLogger : ILogger
{
    public void Log(string message) => Console.WriteLine(message);
}

public class Application
{
    private readonly ILogger _logger;

    public Application(ILogger logger)
    {
        _logger = logger;
    }

    public void Run() => _logger.Log("Application is running.");
}

// Usage (via DI Container in .NET Core)
var serviceProvider = new ServiceCollection()
    .AddSingleton<ILogger, ConsoleLogger>()
    .BuildServiceProvider();

var app = serviceProvider.GetService<Application>();
app.Run();
        

Explanation:

"DI helps in creating loosely coupled systems, making it easier to test and maintain code.

?Question 26: What is the purpose of the async keyword in .NET?

Answer:

"The async keyword is used to define asynchronous methods in C#. It allows methods to run asynchronously without blocking the main thread."

Example:

public async Task<string> FetchDataAsync()
{
    await Task.Delay(1000); // Simulates data fetching
    return "Data fetched!";
}

public async Task Main()
{
    string result = await FetchDataAsync();
    Console.WriteLine(result);
}
        

Explanation:

"Asynchronous programming improves application responsiveness, especially in I/O-bound or network-bound operations."

?Question 27: What are Events in C#?

Answer:

"Events in C# are a way to notify other parts of the program when something happens. They are based on delegates and are used in the publisher-subscriber model."

Example:

public class Publisher
{
    public event Action<string> OnPublish;

    public void Publish(string message) => OnPublish?.Invoke(message);
}

public class Subscriber
{
    public void Subscribe(Publisher publisher)
    {
        publisher.OnPublish += MessageReceived;
    }

    private void MessageReceived(string message)
    {
        Console.WriteLine($"Message received: {message}");
    }
}

// Usage
var publisher = new Publisher();
var subscriber = new Subscriber();
subscriber.Subscribe(publisher);
publisher.Publish("Hello, Event!");
        

Explanation:

"Events are commonly used in GUI applications and real-time systems to notify subscribers when an action occurs."

?Question 28: What is LINQ in C#?

Answer:

"LINQ (Language Integrated Query) is a query syntax in C# used to query data from various sources like collections, databases, and XML documents."

Example:

int[] numbers = { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);

foreach (var number in evenNumbers)
{
    Console.WriteLine(number);
}
        

Explanation:

"LINQ provides a readable and concise way to work with data. It can simplify complex queries and make code more maintainable."

?Question 29: What is a try-catch block in C#?

Answer:

"A try-catch block is used for exception handling in C#. It allows you to catch and handle runtime exceptions."

Example:

try
{
    int a = 10, b = 0;
    int result = a / b; // Will throw DivideByZeroException
}
catch (DivideByZeroException ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}
finally
{
    Console.WriteLine("Execution completed.");
}
        

Explanation:

"This is a fundamental feature in C#, used to gracefully handle errors and prevent application crashes."

?

?Question 30: What is the difference between abstract class and interface?

Answer:

- Abstract Class: Can contain both abstract methods (without implementation) and concrete methods (with implementation).

- Interface: Can only declare methods, properties, events, and indexers without implementation.

Example:

abstract class Animal
{
    public abstract void Speak();
    public void Sleep() => Console.WriteLine("Sleeping...");
}

interface IFlyable
{
    void Fly();
}

public class Bird : Animal, IFlyable
{
    public override void Speak() => Console.WriteLine("Chirp Chirp");
    public void Fly() => Console.WriteLine("Flying...");
}
        

Explanation:

"This question helps interviewers assess your understanding of inheritance and polymorphism, which are crucial in object-oriented programming."

  • #DotNet
  • #CSharpBasics
  • #DotNetInterview
  • #InterviewQuestions
  • #CSharpForBeginners
  • #ProgrammingTips
  • #DotNetCore
  • #TechInterviews
  • #SoftwareDevelopment
  • #CodingJourney

?

?

Zafar Ali

software Engineer | SQL | Angular

3 个月

Very informative

回复
Bilawal Hussain

ASP.Net Core MVC | ASP.Net Core Web API | N-Tier Architecture | Repository Pattern | Unit of Work | EF Core | LINQ | ADO.Net | SQL | C# | OOP | Git | JavaScript | jQuery | Bootstrap | HTML | CSS

4 个月

Very informative

要查看或添加评论,请登录

Asharib Kamal的更多文章

社区洞察

其他会员也浏览了