.NET Interview Questions
Jan 26 2018
- An application’s architecture consists of following layers
- UI layer: web app, wpf, windows gui, etc.
- Business Logic: How your application works
- Data Access Layer: Provides communication with the databases, e.g. EF Core, Repository Pattern
- Common functionalities and libraries
- Explain classes and objects in simple terms
- A class is a template that specifies the data and operations for an entity
- An object is an instance of that class created at runtime using the new keyword
- What is a static class? Why is it used?
- A Static class provides a cohesive set of functionalities/services, and doesn’t contain any internal data
- It has a static keyword in the signatures
- It can only contain static members, i.e. static fields, static properties and static methods
- It doesn’t have an instance constructor, as we don’t need to initialize it
- It acts as a container for utility methods/common code libraries, e.g. logging, email, etc.
- What is a Partial class?
- Partial class allows us to split the definition of a class or a struct, an interface or a method over two or more source files.
- What is a Sealed class?
- A sealed class cannot be used as a base class.
- No other classes can inherit a sealed class.
- Null conditional operator ?.
- Removes the need to explicitly check the null references
- If null then null; if not then dot
- What is lazy loading and when would you use it?
- It is a technique of instantiating objects when they are needed and not before
- Create the instance in the property getter for the related object
- What is an interface?
- It’s a specification for identifying a related set of properties and methods.
- When a class implements an interface, it commits to supporting that specification by implementing code
- By using interfaces, we can write more generalized code when defining method parameters, or method return values
Fields and Properties
- Why use Nullable field?
- Use Nullable field, i.e. string price? to differentiate between not set and default value
- You can use HasValue and Value properties on nullable fields
- What’s the difference in const vs readonly
- const provides compile time constant values. They must be hardcoded, or an expression that evaluates at compile time.
- readonly provides runtime constant values. They can be set via expressions evaluated at runtime, but don’t change them later.
- How can you increase the readability of a method call?
- Use the named arguments, i.e. placeOrder(product, quantity: 12, sendCopy: false)
- What is a delegate?
- A delegate is a type that represents a reference to a method with with a parameter and a return type
- A lambda expression can be passed to a delegate
- What are extension methods?
- Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
- Strings are immutable reference types, that act as value types
- What’s verbatim string literals?
- If you put @ in front of the string, it will print \r or \n as it is, instead of new line
- How do you build large blocks of string?
- Use StringBuilder, it’s a mutable string(conceptually)
- What’s the difference in for vs. foreach in C#?
- foreach is easier to read and write
- for gives us more flexibility. We can update the element, as well as change the order or sequence of iteration
- What are generics? Why are they used?
- Generics is a technique that allows to define variable data types
- Generics allow us to write more reusable code without fixing data types, and at the same time provide type safe collections