C# Flashcards
What is using?
List of directives, specifies namespaces in which the classes/interfaces are located that the code is using, allows one namespace to use another namespace
What are partial classes?
Allows for one class to be spread across several definitions (often in separate files) which increases readability of the code. All parts must use partial keyword and be in the same namespace, each can inherit from different type and all inherit from it.
What are sealed classes?
Sealed classes cannot be inherited from, so can’t be extended but they can inherit from other classes. Static classes can’t inherit and can’t be inherited from.
What is abstract class?
Abstract class is similar to an interface, it provides a blueprint for inheriting classes - defines fields and methods and can implement them but doesn’t have to.. All methods are abstract. The class can’t be instantiated and can only be derived from.
What is string builder and what is different to string?
String builder is a class that helps with string modification. Strings can’t be modified, each time we add something or transform a string a new string is created, while string builder allows for string modification through modification of each character.
Different between as and is?
As is used for casting an object type to another, is is used to check the underlying type of an object.
Heap and Stack
Reference values are stored on heap, values on stack. Stack also stores local variables and method calls. Heap is dynamic but slower.
Can C# classes inherit from multiple classes?
No, they can inherit from one class but multiple interfaces.
Const and readonly
Const has to be assigned to and has the same value during lifetime of the application. Readonly can change value at runtime - doesn’t need to be assigned.
What is everything in C#?
Everything is a class.
What is delegation?
Evaluating one object in the context of another.
What is open recursion?
Allowed by delegation -> we can use open recursion by using this keyword to access the inheriting chain.
What is abstraction?
By using abstract classes or interfaces, we should express the intent of an class without providing implementation. One class shouldn’t know the implementation of another - we can give it a blueprint and tell it what to call without it having to know what exactly happens. Extension of encapsulation in order to maintain code - if too many classes know about each other’s behaviours, it will become impossible to change or maintain code.
What is encapsulation?
Hiding specific implementation by restricting access to it. Objects hide their internal state only expose themselves to others by methods through which they can communicate, for example by using an interface.
What is inheritance?
Objects can have things in common .e.g. share logic, instead of copying code, we can reuse the code by making classes inherit from other classes. Types: single (one base class), hierarchical (one base class, multiple derived classes), multilevel (derived class of derived class), multiple (several base classes, several derived classes).
What is destructor?
Method that destructs an object after it is done being used. Opposite of constructor. It gets called by garbage collector when object is destroyed - frees up resources and memory.
Advantage of using getters & setters
Encapsulation. Internal state is not exposed as if we used a public property. We have control over who has access to what within an object. Allow for extra validation, allows inheritors to override setters and getters.
What are inline functions?
Technique used by compilers that instructs to insert complete body function wherever the function is called in the source code.
What is virtual function?
Function that can be overridden by a derived class.
What is friend function?
Friend of a class that can access field that are private or protected. Must be declared within the class.
What is method overloading?
A function that can complete different tasks depending on the parameters we pass in. The signature must be exactly the same except for the parameters. The implementation can be the same/similar except we use the same params to get to the result.
What is method overriding?
Using inheritance, we can override the behaviour of a method in the parent class The signature must be identical but the implementation can be different.
Difference between overriding and overloading methods
Overriding uses inheritance to allow child classes to have different implementations. Overloading is defining several methods whose implementation differs based on the parameters we send.
What is operator overloading?
Providing special meaning/implementation to operators.
What is virtual pure function?
It is a virtual method that must be overridden in the child class - doesn’t have any implementation in the parent class -> abstract class and an abstract method.
What is the finalize function?
Called by garbage collector to used to cleanup unmanaged resources before the object is destroyed.
What are two types of arguments?
Passed by value and passed by reference. Reference type arguments are mutable. Value type arguments are destroyed when the method is done executing.
DATA STRUCTURE: What is heap?
Complete binary tree. Either minimum or maximum tree, root will be smallest or largest node in the tree.
Which data structures use pointers?
Linked lists, stack, queu, binary trees.
Full vs complete binary tree
Full - each node other than leaves have two children.
Complete - every level except the last one is completely filled - all nodes are left as possible.
What is infix?
It’s an operator position - between elements - A*(B+C).)
What is prefix?
It’s an operator position - before operator - /*A+B.
What is postfix?
It’s an operator position - follows after each operator - ABC+*D/.
How to implement LRU cache?
Last recently used. The least recent should be removed first -> browser keeps track about last visited pages in a queue, double-linked list, hash.
Call stack
Stack saves the information about the last function called and pushes it to the bottom, once it’s done performing, the highest possible function it goes through the function stack.
Void vs NULL
Technically, null is a value since it points to a location in memory. Void does not point at anything - data type identifier that means nothing.
Most common searching algorithms
Binary search, linear search.
What are most common sorting algorithms?
Selection sort, bubble sort, merge sort, radix sort, heap sort, quick sort.
What are fibonacci numbers?
It is a series of numbers starting with 0 ad 1 with the following number being a addition of the two previous numbers. 0 1 1 2 3 5
What is linear search?
Search in sequence, each element is searched and compared against the searched key.
How does binary search in binary tree?
If tree is empty - the search ends. If the tree not empty, check if root element is it, if not check left of root, then check left of that node, then left until there is none, then go right.
What is merge sort?
Divide and conquer. Divides an array in half and first sorts each half by more halving until we have just two elements. Adjacent elements of sequence sorted first and merged. O(nlogn)
What is bubble sort?
Compares adjacent elements and exchanges their values, if they are out of order - largest items sink to the bottom of the list. The smallest value bubbles up to the start. O(n^2)
What is quick sort?
Pick random element and partition array such that all numbers are less than the element that comes before all elements that are greater than it. Forms subarrays to do it. It’s not great since the random element can be in bad position.
What is selection sort?
Smallest number is first located and switched with the element at 0 index. Then the second smallest position is put at the second position but that can mean it goes through each element at lesat once. O(n^2)
What is radix sort?
Sorts ints, first looks at first digits, the same ones are grouped together, that continues until all sorted
JIT vs AOT
JIT - compiles IL to machine code, AOT - ahead of time delivers fast startup in large apps but needs more disk space and memory
Methods vs functions
Methods are functions belonging to a class - can operate on the data of the object. Functions need all arguments passed in.
What are mixins?
Contains methods used by other classes without it being parent class of the other classes, it’s more included than inherited from.
What is a monad?
Abstracting boilerplate code by passing in their own data type e.g. generics -> List where T is monad
What is roslyn?
Compiles C# into Intermediary language.
What is RUYJIT?
Compiles IL into machine ode.
Do you modify access to interface?
No, can include public identifier but it’s public by default.
Stack vs Heap Memory
Stack stores value types, function arguments, keeps stack call of methods. Heap - where reference types are stored, keeps track of objects.
What is .Net web service?
Reusable piece of code that allows for publishing things over the internet and interacting with other resources over HTTP, XML, SOAP. ASP.Net provides simple way of implementing them.
Why should we prefer MVC over Web Forms?
Web forms are outdated and not well maintained plus MVC allows for separation of concerns.
What is Json in relation to .Net?
JavaScript object notation. A way to depict data through api calls, easy to read and organise data. Libraries to parse json strings for storage etc.
What is Laravel?
PHP version of .NET, open source framework based on MVC.
.Net vs .Net Core
.Net Core is the open source, cross-platform, NUGGET based subset of .Net. They have shared common libraries.
What is included in .Net core?
.Net runtime, type system, assembly loading, garbage collector, set of framework libraries, SDK tools & language compiler, dotnet app host.
Does .NET Core have the same libraries?
It shares some base libraries with .Net, except they are shipped as smaller libraries via nugget.
What is Core CLR?
Common language runtime execution engine that takes care of compilation and garbage collection.
What is Mono and how does it differ to Core?
Mono is a 3rd party library, implementation of .Net Core for other platforms before Microsoft created Core.
Characteristics of Core
Cross-platform, open source, can be shipped over nugget, flexible deployment, command line tools, compatible with .Net, developed by Microsoft.
What is CTS?
Common type system. System for data types shared by most Microsoft languages/frameworks, to ensure smooth communicate between all .Net languages. All types inherit from System.Object
How to sort array?
Sort(), Reverse()
What is circular reference?
Trying to reference a project from a project that is referenced by that project. Resources are dependent on each other and create lock condition and the resources can’t be used.
What are generics?
Placeholders for type of fields, methods, parameters, gets specific type at compile time. Why - reusable code, type safe.
What is object pool?
The pool of objects currently being used in code, reduces overhead of creating and recreating objects.
What are delegates?
Instead of using a specific method, we can pass in a blueprint of the method (the signature) without having to define or name the method yet.
What is constructor chaining?
Calling constructor from another constructor - e.g. child constructor calling parent.
What is enum?
Value type. Predefined list of possible values = named constants.
What are nullable types?
Value types can’t normally be null unless we add the null operator which allows it to be nullable.
What is null coalescing operator?
If first operand is null, the second one will be assigned. var i = null ?? this;
What is static constructor?
Static constructor gets called to initialize static data before the first instance is created.
What are attributes?
Reusable code blocks that assign some sort of behaviour to class which is evaluated at runtime, method, property etc. [Route], [DataContract],[HttpGet]
What is a thread?
It’s a flow of control - set of instructions to be executed, tasks are executed in the thread - by default, C# only has one but more can be created - multi-threading -> could cause deadlock or circular reference.
How to hide method of pareint in child?
By using the new keyword and implementing the parent method with new implementation but same signature.
Abstract vs virtual
Abstract classes cannot be implemented by parent, must be implemented by child classes, in abstract classes, virtual by default. Virtual methods must have implementation in parent, don’t have to be implemented in child.
What is System.IO?
Library namespace that contains functionality for reading, writing to the file system - StreamWriter, StreamReader.
What are regular expressions?
Patterns that we can use to abstract what we expect e.g. a string to look like - template to match a set of input.
Async vs Sync
Program can be executed async or sync. Sync execution follows chronologically line by line and waits for all code to complete before it returns (only one thread can access resource), while async returns immediately so that program can perform other operations while the code executes.
Do we have to declare private fields?
No, they are private by default, unless we defined getters and setters?
What is serialization?
Process of converting data to the expected data format. Most common: JSON, XML, binary.
What is reflection?
Able to access metadata of objects - e.g. assembly information during runtime and informs user or modifiers behaviour.
What is IEnumerable?
Interface for collections that contains GetAwaiter() which allows to iterate through the collection. Base class for collections, IQueryable implements it too.
IEnumerable vs IQueryable?
IQ derives from IE. IE keeps in memory the object while it is filtered. IQ filters through the collection without saving it in memory. IE is better for LINQ to objects, while IQ is better LINQ to anything else.
What are static classes?
Classes that can’t be instantiated, can’t be inherited from. If class static, all members are static. Static members are on heap. It’s members can be accessed anywhere. All members also static, no this since there is no instance.
Exception handling in C#
try, catch, finally - only one catch per exception type.