Interview Flashcards
Class vs Struct vs Record
Class is a reference type, struct is a value type, record is a reference type. The record is immutable used primarily for DTOs and one way operations. Classes can be inherited. Structs cannot be inherited.
Value type vs reference type
A value type is most commonly stored on the stack. A reference type is stored on the heap type of memory. Depends on the context the variable is declared. Local value types are stored on the stack. Objects of reference variables are stored on the heap. If the value is declared inside a class it will be on the heap.
TLDR: Value types and reference types go to a different type of memory.
Abstract class vs interface
Abstract classes can contain implemented methods and interfaces can’t. C# 8 interfaces can have default implementation. Abstract classes cannot be instantiated but can have all members of classes (props, methods, ctrs, fields)
Abstract classes are used to define a base class while interfaces are used to define an implementation. Classes can implement multiple interfaces while can inherit from only one class.
OOP principles
Encapsulation, abstraction, inheritance, polymorphism
Encapsulation - data implementation is done through public methods and enclosed in classes
Abstraction - classes don’t need to know the actual implementation of other classes they need to be based on their abstractions, abstract classes or interfaces
Inheritance - inheritance provides the ability for classes to reuse code from upper base classes. It also provides the ability to polymorphism
Polymorphism- This is the so called overloading and overriding of methods. Overloading is achieved same name, different number of parameters, overriding is achieved through overriding virtual methods.
Managed vs unmanaged code
Code that executes under the control of the runtime is managed code and is eligible for garbage collection. Unmanaged code is code that is not under control and has to be disposed of. COM components, ActiveX interfaces and Windows API functions are such examples. Unmanaged code is executed by the OS directly
Const vs readonly
Const is assigned value in compile time. Readonly is assigned value in runtime.
IDisposable pattern
Basic Dispose pattern - implementing IDisposable and in the Dispose method dispose of any resources. Because finalizing methods in unpredictable you need to check whether the Dispose method is called from Dispose(bool) or it is called from the finalizer in order to avoid disposing of something already disposed of
Garbage Collection
Background process that collects unused references and frees up memory. There are three conditions that trigger garbage collection:
Low physical memory, low memory on the heap or gc.collect
Garbage collection generations
Gen 0 - youngest objects, short lived objects
Gen 1 - short lived objects buffer to long lived objects. If the gc does not free enough memory from gen 0 objects it collects from gen 1 and then goes to gen 2.
Gen 2 - long lived objects here, static data for example that lives during the whole process.
LOH (Gen 3) - objects larger than 85 MB objects in this gen are collected with gen 2 objects
.NET Framework vs .NET Core / 5,6,8
.NET Framework is a development framework offers API development MVC etc. / .NET Core is a platform that includes ASP.NET, UWP / .NET Core is an open source platform / .NET Framework is not open source
.NET Core is cross platform it can run on all platform and supports all kind of development / NET Framework is used solely for windows development. / .NET Core is shipped as a collection of Nuget Packages / .NET Framework is installed as a single app installer
Offers scalability and performance with comparison to Framework /
Core is compatible with all operating systems /
.NET Core comes up with a very lightweight CLI, it can be used in IDE
.NET Framework is heavy on the CLI
.NET Framework has the Code Access Security
.NET Standard
.NET Standard is a set of APIs compatible with all .NET versions . If you need a portable library that’s compatible with Framework, Core and Xamarin then use the .NET Standard.
async and await
IO bound operations and CPU bound operations
Waiting for some data -> io bound operation
Expensive computation -> cpu bound operation
Task.WhenAll and Task.WhenAny
Multiple tasks return when the unwrapped results of all tasks
when any returns the
Blocking the current thread with Task
Task.Wait or Task.Result, Task.WaitAll, Task.WaitAny, Thread.Sleep
Task vs Thread
A task can have multiple processes, a thread is just a process
A Task returns a result / Tasks can be chained / Tasks propagate exceptions / Tasks are using Thread Pool threads
A Thread is a lowlevel single process
Concurrency vs Parallelism
Concurrency is running on a single CPU while Parallelism is running on multiple processors
Array vs List
Array is fixed size, List doubles its size and allocates its memory /
Arrays generally have better performance, Arrays need to have default values
LinkedLists
LinkedList is a data structure every element is chained to the previous one and the next one. LinkedLists contain members that have pointers to each other but can allocate different parts of the memory unlike arrays.
EF core load related entities
Eager loading -> include / initial query
Explicit Loading -> the related data is loaded at a later time /
context.Entry(blog).Collection( –load some prop–)
Lazy Loading -> the related data is transparently loaded when the navigation property is accessed / LazyLoading requires using a package Microsoft.EFCore.Proxies /lazy loading loads the entities only when accessed
Gang of Four Design Patterns
Creational, Structural, Behavioral patterns