C# Flashcards
What is a class?
A class is an encapsulation of attributes and methods used to represent an entity.
What is an object?
An object is an instance of a class, stored in memory.
What are the fundamental OOP concepts?
Encapsulation, Abstraction, Inheritance, Polymorphism. (EAIP)
What is Encapsulation in OOP?
The ability to define private attributes and methods on a class. This then allows us if we wish, to hide the internal representation/information about an object, from the outside world. Setters & getters are required to access the internal data.
What is Abstraction in OOP?
The concept of moving focus from the concrete implementation of things, to the types of things (classes) and operations available (methods). This allows us to make programming simpler and more abstract. For example, we can have an abstract Animal class which Dog & Cat implement from. Let the programmer focus on Animal rather than Dog or Cat specifics.
What is Inheritance in OOP?
The ability to create new classes from another class by modifying and extending the behaviour of the parent class. For example, Dog & Cat can inherit from an Animal class and we can extend the Dog class with a Woof() method.
What is Polymorphism in OOP?
The ability to have multiple methods with same name, but different implementations.
What is C#?
C# is an object-oriented, type-safe language that is compiled by the .NET framework. The .NET framework provides a run-time environment called the Common Language Runtime (CLR) which runs the code.
Important features of C#?
- It is an OOP strongly-typed language.
- It is type safe, meaning it doesn’t allow unsafe casts like converting a double to a Boolean.
- It can be used to develop console applications, windows applications and web applications.
- With .NET Core, it can now be used to write applications that are cross-platform.
What is the CLR?
The Common Language Runtime (CLR) is a run-time environment which runs C#/.NET code. It compiles it down to intermediate language, therefore making C#, managed code. It contains Garbage Collection (GC): Responsible for automatic memory management.
What is unmanaged code?
Code that is executed by any runtime other than .NET. The ‘other’ runtime will take care of memory, security and other operations.
Can multiple catch blocks be executed?
No. Once an exception has been caught and a single catch block is executed, it then moves to the finally block.
What is the use of ‘using’ statements in C#?
Once the code within the using statement has been executed to obtain a resource, it is then automatically disposed of. e.g. Dapper/IDbConnection to close the connection. This uses IDisposable in the background and calls .Dispose() on it.
What is a Jagged Array?
A Jagged Array is an array whose elements, are arrays.
What is the difference between an Array and an ArrayList?
Arrays have a fixed size, ArrayList’s are dynamic.
What is the difference between String and StringBuilder?
String is immutable so when we modify it, a new instance is created in memory. StringBuilder however is mutable so the same string in memory is modified.
What is the difference between public, static and void?
Public declared variables or methods are accessible anywhere within the application. Static is the same except can be called without an object instantiation. Void is a type modifier that a method does not return any value.
What are sealed classes in C#? What are sealed methods or properties in C#?
When a class is sealed, it prevents other classes inheriting from it.
Using sealed on a method or property in a class will allow other classes to derive from the class but prevent them being overridden.
What is an interface class? Give an example.
An interface is an abstract class which defines the contract of a class. It contains declaration of public methods to satisfy the contract. If a class implements an interface, it must abide by that contract. An example is: IVehicle, Car : IVehicle, Boat : IVehicle
What is the difference between an interface & abstract class?
Interfaces are just contracts and contain no definition or implementations of public methods. Abstract classes meanwhile can have concrete implementations of methods along with private methods.
Give an example of using an abstract class.
abstract class FourLeggedAnimal -> string Describe() { return “I am a four legged animal” } Dog : FourLeggedAnimal -> Dog.Describe(); // inherits the implementation
What is method overloading?
When we create multiple methods with the same name, but different parameter options.
Can private methods be overridden?
No as they are not visible outside the scope of their class.
What is the difference between private and protected?
Private variables or methods cannot be accessed by child classes, only internally. Protected variables & methods are accessible by both child classes & interally.
What is the difference between a ref parameter & an out parameter?
A ref parameter must be initialised prior to being passed to a method. An out parameter needs not.
Can we use ‘this’ in a static method?
No because we can only use static variables and methods in a static method.
What is the difference between const and readonly?
Constant variables are declared and initialised at compile time where the value cannot then be changed without recompilation. Read Only variables within a class meanwhile can be changed, but only via a constructor during runtime.
What is the difference between a value type and a reference type?
An integer is a value type as it holds a data value in its own memory space. A string is a reference type as it holds a reference/pointer to a different memory space where data is stored.
What is a protected class/method/variable?
Protected means something is only visible to itself & derived/child classes.
What is serialization?
When we want to transport an object through a network, we convert it into a stream of bytes to send. For an object to be serializable, it must implement ISerialize. Types of serialization include JSON serialization, XML serialization, SOAP or Binary serialization.
What is a virtual method? Give an example.
Virtual allows you to indicate that a method or property can be overridden within a derived class. abstract class FourLeggedAnimal -> virtual string Describe() { return “I am a four legged animal” } Dog : FourLeggedAnimal -> string Describe() { return “Woof” } -> Dog.Describe(); // overridden
What are circular references/dependencies?
When two or more resources/packages depend on each other. Since one can not be compiled without the other, this makes both unusable until the circular references are resolved.
What are generics in C#?
Generics are used to decrease code redundancy, increase type safety and performance. For example, new List();
What are delegates in C#?
Like C++ function pointers, a delegate is a variable that references a potential method. It can be passed as a parameter to another method and used to trigger that method.
What is a multicast delegate?
A delegate that has multiple handlers assigned to it.
What is the difference between a Struct and a Class?
Structs are value-type whilst Classes are reference-types. Structs store their attributes on a stack, whilst Classes store their attributes on a Heap. All members of a struct are public by default, whilst all members of a class are private by default. Structs do not support inheritance. As structs are value-type, there is no need for the Garbage Collection to deal with them.
What is the Singleton Pattern and how can you implement it in C#?
A singleton is a class which only allows a single instance of itself to be created. public sealed class Singleton { private static readonly Singleton _instance = new Singleton(); private Singleton() {} public static Singleton Instance { get { return _instance; }} }
What is the difference between a Singleton class and a Static class?
A static class cannot be extended, whereas a singleton can.