C#_1

 

C# Interview Questions for Experienced Developers (3+ Years)

This document compiles common interview questions and topics for C# developers with 3+ years of experience. It focuses on mid-level to senior topics including advanced OOP, LINQ, async programming, Entity Framework, ASP.NET Core, design patterns, SOLID principles, dependency injection, unit testing, performance optimization, and security.

Advanced OOP Concepts

  • What are partial classes in C#?
    Partial classes allow splitting the definition of a class across multiple files. They are useful for separating auto-generated code from user code.

  • Difference between String and StringBuilder.
    String is immutable; each modification creates a new string. StringBuilder is mutable and efficient for multiple concatenations.

  • Const vs Readonly.
    Const values are compile-time constants. Readonly values can be set at runtime (in constructor) and are instance-specific.

  • What is Reflection in C#?
    Reflection allows inspecting and manipulating types, methods, and assemblies at runtime using System.Reflection namespace.

  • Method Overloading vs Overriding.
    Overloading: Same method name, different parameters. Overriding: Subclass provides specific implementation of inherited method.

  • Equality Operator (==) vs Equals() Method.
    == compares references for reference types, values for value types. Equals() compares values.

  • What are Indexers in C#?
    Indexers allow objects to be indexed like arrays using this[] syntax.

  • Late Binding vs Early Binding.
    Early binding: Method calls resolved at compile-time. Late binding: Resolved at runtime, using dynamic or reflection.

  • Properties in C#.
    Properties provide controlled access to private fields using get/set accessors.

  • Boxing and Unboxing.
    Boxing: Converting value type to object (reference type). Unboxing: Converting object back to value type.

LINQ (Language Integrated Query)

  • What is LINQ?
    LINQ allows querying data from various sources (collections, databases, XML) using C# syntax.

  • Deferred Execution in LINQ.
    Queries are not executed until enumerated, allowing optimization.

  • Difference between IEnumerable and IQueryable.
    IEnumerable for in-memory collections. IQueryable for database queries, supports expression trees.

  • Common LINQ Operators.
    Where, Select, GroupBy, Join, OrderBy, etc.

  • LINQ to SQL vs LINQ to Entities.
    LINQ to SQL for SQL Server. LINQ to Entities for Entity Framework.

Async Programming

  • What is async/await?
    Async methods run asynchronously without blocking the calling thread. Await suspends execution until the task completes.

  • Difference between Task and Task.
    Task for void-returning operations. Task for operations returning a value.

  • ConfigureAwait(false).
    Prevents capturing the synchronization context, improving performance in library code.

  • Handling Exceptions in Async Code.
    Use try/catch around await or AggregateException for Task.Wait().

  • Deadlocks in Async Code.
    Occur when blocking on async code in UI threads. Avoid .Wait() or .Result on UI threads.

Entity Framework

  • What is Entity Framework?
    ORM for .NET, maps objects to database tables.

  • Code First vs Database First.
    Code First: Define model in code, generate DB. Database First: Generate model from existing DB.

  • Lazy Loading vs Eager Loading.
    Lazy: Load related data on access. Eager: Load with main query using Include().

  • Migrations in EF.
    Track and apply schema changes to DB.

  • DbContext and DbSet.
    DbContext manages DB connection. DbSet represents entity collections.

  • Change Tracking.
    EF tracks changes to entities for automatic updates.

ASP.NET Core

  • What is ASP.NET Core?
    Cross-platform, open-source framework for building web apps.

  • Middleware in ASP.NET Core.
    Components that handle requests/responses in the pipeline.

  • Dependency Injection in ASP.NET Core.
    Built-in DI container for managing services.

  • Razor Pages vs MVC.
    Razor Pages: Page-focused. MVC: Controller-based.

  • Authentication vs Authorization.
    Authentication: Verify identity. Authorization: Check permissions.

  • Kestrel vs IIS.
    Kestrel: Cross-platform web server. IIS: Windows-specific.

Design Patterns

  • Singleton Pattern.
    Ensures single instance of a class.

  • Factory Pattern.
    Creates objects without specifying exact classes.

  • Observer Pattern.
    Notifies multiple objects of state changes.

  • Repository Pattern.
    Abstracts data access logic.

  • Dependency Injection Pattern.
    Injects dependencies rather than creating them.

SOLID Principles

  • Single Responsibility Principle (SRP).
    A class should have one reason to change.

  • Open/Closed Principle (OCP).
    Open for extension, closed for modification.

  • Liskov Substitution Principle (LSP).
    Subtypes should be substitutable for base types.

  • Interface Segregation Principle (ISP).
    Clients should not depend on unused interfaces.

  • Dependency Inversion Principle (DIP).
    Depend on abstractions, not concretions.

Dependency Injection

  • What is DI?
    Technique for achieving loose coupling by injecting dependencies.

  • Constructor Injection vs Property Injection.
    Constructor: Required dependencies. Property: Optional.

  • DI Containers.
    Frameworks like Autofac, Ninject for managing DI.

  • Service Lifetimes.
    Transient, Scoped, Singleton.

Unit Testing

  • What is Unit Testing?
    Testing individual units of code in isolation.

  • Mocking Frameworks.
    Moq, NSubstitute for creating test doubles.

  • Arrange-Act-Assert.
    Pattern for structuring tests.

  • Test-Driven Development (TDD).
    Write tests before code.

  • Code Coverage.
    Measure of code tested by unit tests.

Performance Optimization

  • String Interning.
    Reuse string instances to save memory.

  • Object Pooling.
    Reuse expensive objects.

  • Asynchronous Programming.
    Avoid blocking threads.

  • Memory Leaks.
    Unmanaged resources, event handlers.

  • Profiling Tools.
    Visual Studio Profiler, dotTrace.

Security

  • SQL Injection Prevention.
    Use parameterized queries.

  • Cross-Site Scripting (XSS).
    Encode output, validate input.

  • Authentication Methods.
    JWT, OAuth, Windows Auth.

  • Encryption.
    Use HTTPS, encrypt sensitive data.

  • Input Validation.
    Validate and sanitize user input.

This list is comprehensive but concise. Use it for preparation and practice explaining concepts with examples.

Comments

Popular posts from this blog