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.