C# Flashcards
(96 cards)
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.