C#/.Net Flashcards
(150 cards)
String vs stringbuilder
String is immutable - if you modify it, a new string is created.
SB is mutable, any manipulation done on it, won’t create a new instance each time.
If you want to change a string multiple times, SB is a better option from performance pov BC it won’t require new memory each time.
Covariance Vs contravariance
Covariance (out)
- allows a method to return a more derived type than specified by the generic parameter.
- supports assignment compatibility from more derived to less derived types (ie derived to base)
- used mostly in return types
Example:
IEnumerable<string> strings = new List<string>();
IEnumerable<object> objects = strings; // Valid because IEnumerable<out> is covariant</out></object></string></string>
Contravariance (in)
- allows a method to accept parameters of less derived types than specified by the generic parameter.
- supports assignment compatibility from less derived types to more derived types (ie Base to Derived)
- used mostly in input parameters
Action<object> actObject = obj => Console.WriteLine(obj);
Action<string> actString = actObject; // Valid because Action<in> is contravariant</in></string></object>
What is a value type in C#?
A value type holds data directly, is stored on the stack, and a copy is made when assigned to another variable. Examples: int, float, bool, struct, enum.
What is a reference type in C#?
A reference type holds a reference to data stored on the heap. When assigned, the reference is copied, so both variables point to the same object. Examples: class, string, array, object, delegate.
Where are value types stored in memory?
On the stack.
Where are reference types stored in memory?
On the heap (the reference itself is on the stack).
Can value types be null?
No, unless they are nullable using ?, e.g., int
Can reference types be null?
Yes
What happens when you assign one value type variable to another?
A copy of the value is made. The two variables are independent
What happens when you assign one reference type variable to another?
The reference is copied. Both variables point to the same object in memory.
What is the difference between an array and a List in C#?
An array has a fixed size and is defined using [], while a List (List<T>) is dynamic and can grow or shrink. Lists offer more built-in methods (like Add, Remove, Contains) and are generally easier to work with, while arrays are slightly more performant and memory-efficient for static data.</T>
What is a dictionary/map, and when would you use it?
A Dictionary (or Map) in C# is a collection of key-value pairs where each key is unique. It allows fast lookups, additions, and deletions by key.
Use it when you need to quickly access data by a specific identifier (like a name, ID, or key).
What is a singleton?
A Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to it. It’s commonly used for things like configuration, logging, or shared resources.
What is the difference between Dictionary and HashSet in C#?
A Dictionary<TKey, TValue> stores key-value pairs, where each key is unique and maps to a value.
A HashSet<T> stores only unique values, with no key—just the values themselves.</T>
Use Dictionary when you need to look up a value by a key.
Use HashSet when you only care about uniqueness and fast existence checks.
How does a static field differ from a non-static field?
A static field belongs to the class itself and is shared across all instances. A non-static field belongs to a specific instance of the class.
How would you call a static method vs an instance method?
A static method is called using the class name (e.g., ClassName.Method()), while an instance method is called on an object (e.g., object.Method()).
What is boxing and unboxing in C#?
Boxing is converting a value type to an object. Unboxing is extracting the value type from the object.
What is a namespace and what is its purpose?
A namespace is a way to organize code and avoid naming conflicts by grouping related classes, interfaces, and types.
What are the access modifiers in C#, and how do they differ?
public: accessible from anywhere.
private: accessible only within the same class.
protected: accessible in the class and its subclasses.
internal: accessible within the same assembly.
protected internal: accessible in the same assembly or derived classes.
private protected: accessible in the containing class or derived classes in the same assembly.
What is the using statement used for, and what interface supports it?
The using statement ensures that resources are disposed automatically. It works with types that implement IDisposable.
What is the difference between abstract classes and interfaces in C#?
Abstract class can provide both implementation and abstraction.
Interface provides only abstraction (no implementation, until C# 8).
Use an interface for capabilities across unrelated classes; use an abstract class for base functionality in related classes.
How does async/await work, and what is its purpose?
async/await allows for asynchronous programming without blocking the main thread. await pauses execution until the awaited task is complete, while async marks a method to support await.
What is the yield keyword used for in C#?
yield is used to return elements one at a time in an iterator block. It is commonly used with IEnumerable to implement custom iteration.
What LINQ method filters results?
Where()