Classes and members (C#) Flashcards

1
Q

What does object orientated programming (OOP) mean?

A

It is when a programming language organizes code into objects, which are instances of classes.

It focuses on representing real-world entities as objects and their interactions through methods, properties, and events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which concepts does OOP emphasize?

A
  1. Encapsulation.
  2. Inheritance.
  3. Polymorphism.
  4. Abstraction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between an object-oriented language and a non-object-oriented language?

A

An object-oriented language is designed to support object-oriented programming principles and features. It provides built-in constructs like classes, objects, inheritance, and polymorphism.

Non-object-oriented languages do not have these built-in constructs and instead rely on other programming paradigms like procedural or functional programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is C# object-oriented?

A

It supports the fundamental principles of OOP.

It provides features like classes, objects, inheritance, polymorphism, encapsulation, and abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are classes in C#?

A

A class is a blueprint or template that defines the characteristics (data members) and behaviors (methods, properties, events) of objects.

It serves as a blueprint for creating instances of objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are structs in C#?

A

They are user-defined types in C#. However, they are value types rather than reference types like classes.

Structs are typically used for lightweight objects that don’t require inheritance or complex behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are records in C#?

A

They are a brief way of defining immutable data types.

They automatically generate useful methods such as equality comparison, hashing, and string representation based on their properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we use records in C#?

A

public record Person(string Name, int Age);

// Create a new instance of the record
var person = new Person(“John”, 30);

// Access properties
Console.WriteLine(person.Name); // Output: John
Console.WriteLine(person.Age); // Output: 30

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are abstract classes in C#?

A

It is a class that cannot be instantiated directly.

It serves as a base class for other classes and provides a common interface or set of behaviors that derived classes must implement.

Abstract classes can contain both regular and abstract members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are abstract members C#?

A

They are declared within an abstract class but do not provide an implementation.

Derived classes must override these members to provide their own implementation.

Example of abstract members:
Abstract methods or properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the keyword “static” mean/do?

A

It is a keyword used to declare members (fields, methods, properties) that belong to the class itself rather than instances of the class.

Static members are shared among all instances of the class and can be accessed without creating an object of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is evolving classes in C#?

A

?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are partial classes in C# and why do we use them?

A
  1. It is a class that can be split into multiple source code files. The partial keyword allows a class’s definition to be spread across multiple files, making it easier to organize and maintain large classes.
  2. Partial classes are often used in code generation scenarios or when multiple developers need to work on different parts of a class simultaneously.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the keyword “partial” mean/do?

A

It allows a class, struct, or interface to be defined in multiple files.

It allows the definition of a type to be split across multiple source code files, making it easier to organize and maintain large classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the 4 object-oriented features?

A
  1. Encapsulation.
  2. Abstraction.
  3. Inheritance.
  4. Polymorphism.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is encapsulation and why use it?

A

It is the practice of bundling data and methods within a class and controlling access to them.

We use it, because it helps to protect data, organize code, and make it easier to maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is abstraction and why use it?

A

It simplifies complex entities by focusing on essential aspects and hiding unnecessary details.

We use it because it helps to manage complexity, promote modularity, and create reusable code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is inheritance and why use it?

A

It is a feature in object-oriented programming where a class can inherit properties and behaviors from another class.

We use it because it promotes code reuse, reduces duplication, and enables hierarchical organization and specialization of objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does specialization and generalization mean in relation to inheritance?

A

Specialization: This means creating specific classes from a general base class, inheriting and extending its properties and behaviors to meet specific needs.

Generalization: This means creating a more general base class by extracting common properties and behaviors from multiple specialized classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does it mean that inheritance is transitive?

A

It means that if class B inherits from class A, and class C inherits from class B, then class C also indirectly inherits all the members of class A.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How does constructors work in inheritance?

A

Constructors are not inherited from the base class to the derived class.

However, when a derived class is instantiated, the constructor of the base class is called first to initialize the inherited members, and then the derived class’s constructor is executed to initialize its specific members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is “preventing” inheritance and what does the keyword “sealed” mean/do?

A

The keyword “sealed” can be used to prevent a class from being inherited. When a class is marked as “sealed,” it cannot be used as a base class for other classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is polymorphism and why use it?

A

It allows objects of different types to be treated as if they were of the same type.

We use it because it provides code flexibility and enables generic programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is member hiding in polymorphism?

A

It is the act of declaring a member with the same name in a derived class to hide the base class member.

25
Q

What does the keyword “new” mean/do in relation to member hiding?

A

It is used to explicitly indicate the intention of hiding the base class member.

26
Q

What are the 5 SOLID principles?

A
  1. Single responsibility principle
  2. Open/closed principle.
  3. Liskov Substitution Principle.
  4. Interface Segregation Principle.
  5. Dependency Inversion Principle.
27
Q

Explain the 5 SOLID principles.

A

Single responsibility principle: A class should have only one reason to change, meaning it should have a single responsibility or purpose.

Open/closed principle: Software entities (classes, modules, etc.) should be open for extension but closed for modification. This means that you should be able to add new functionality without modifying existing code.

Liskov Substitution Principle:
Derived classes should be able to replace their base classes without altering the correctness of the program.

Interface Segregation Principle:
Clients should not be forced to depend on interfaces they don’t use.

Dependency Inversion Principle:
High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.

28
Q

What is visibility and why do we use it?

A
  1. Visibility refers to how accessible members (such as fields, methods, and properties) in a class are. It determines who can access them and from where.
  2. We use it, because it helps control access to sensitive information and keeps implementation details hidden.
29
Q

What is an access modifier?

A

It specifies the accessibility level of a member.

30
Q

Name and explain alle the access modifiers in C#.

A

public: The member is accessible from any other code.

private: The member is accessible only from within the same class.

protected: The member is accessible within the same class and its derived classes.

internal: The member is accessible within the same assembly (project).

protected internal: The member is accessible within the same assembly and its derived classes, both within and outside the assembly.

31
Q

Can you name some members in a class?

A
  1. Fields.
  2. Methods.
  3. Properties.
  4. Events.
  5. Nested types (nested classes, structs, etc.).
32
Q

What are fields?

A

They are variables that hold data within a class.

They store the state of an object and provide the class with its attributes and data values.

33
Q

What does it mean when a field is marked as “read-only”?

A

It means that its value can only be assigned during object initialization or within the constructor.
Once assigned, it cannot be modified.

34
Q

How does fields grow into properties?

A

They can be encapsulated and exposed through properties. Properties provide a level of abstraction and allow controlled access to the underlying fields.

35
Q

What are constants?

A

They are fixed values that do not change during program execution.

36
Q

What is the subtle difference between const and read-only fields?

A

“Const” fields are evaluated and assigned at compile-time, while “read-only” fields can have their values assigned at runtime, typically within the constructor or object initialization.

37
Q

What are properties?

A

They are like special fields that have getter and setter methods.

With properties, you can get and set values as if you were directly accessing fields, but you can also add custom logic and validation.

38
Q

How do we use properties?

A

?

39
Q

What are getters and setters?

A

Getters retrieve the value of a property.

Setters modify the value of a property.

40
Q

What does it mean when an object is initialized with “init”?

A

It means that the object can only be modified during object initialization but not afterward.

It provides a way to create immutable objects where the initial state is set during construction and remains unchanged.

41
Q

What are some auto-implemented properties?

A

They provide a shorter way to define properties without writing extra code for the backing fields.

Example:
public string Name { get; set; }

42
Q

What are constructers?

A
  1. They initialize objects of a class.
  2. They are called automatically when an object is created and typically allow you to set initial values for the object’s properties.
  3. They have the same name as the class and may have parameters.
43
Q

What does the keyword “this” mean/do?

A

It refers to the current instance of the class being constructed.

It helps differentiate between instance variables and parameters with the same names.

44
Q

What are de-constructers?

A
  1. They are also know as finalizers.
  2. They clean up resources when objects are destroyed.
45
Q

What are finalizers?

A

They are special methods called when an object is being destroyed to release resources or perform cleanup tasks.

46
Q

What are methods?

A

They are functions in a class that perform specific tasks.

47
Q

What does the keyword “override” mean/do?

A

It indicates that a method in a derived class replaces the implementation of the same method in the base class.

48
Q

What is method hiding?

A

It happens when a method in a derived class has the same name as a method in the base class but provides a new implementation.

49
Q

What is static method dispatch and how do we use it?

A

It happens when a method in a derived class has the same name as a method in the base class but provides a new implementation.

Example:
public class BaseClass
{
public virtual void SomeMethod()
{
Console.WriteLine(“BaseClass SomeMethod”);
}
}

public class DerivedClass : BaseClass
{
public new void SomeMethod()
{
Console.WriteLine(“DerivedClass SomeMethod”);
}
}

class Program
{
static void Main(string[] args)
{
BaseClass derivedObjAsBase = new DerivedClass();
derivedObjAsBase.SomeMethod(); // Output: “BaseClass SomeMethod”
}
}

50
Q

What is virtual methods and explain the keyword virtual?

A

Virtual methods can be overridden by derived classes, and the “virtual” keyword allows this behavior.

51
Q

What are indexers?

A

They allow accessing elements or values in an object using index notation, like arrays.

52
Q

What are operators (overload)?

A

It lets you redefine the behavior of operators (+, -, *, /, etc.) for custom types.

53
Q

What are events?

A

They enable objects to notify other objects when specific actions or conditions occur.

54
Q

What are types?

A

They classify values or entities based on their characteristics or properties.

55
Q

What are anonymous types?

A

They allow creating objects without explicitly defining a named class.

56
Q

What are objects and how do we use them?

A
  1. Objects are instances of classes and can be created with the “new” keyword.
  2. They can be accessed, modified, and used to perform actions.
57
Q

What are object initializers?

A

They set initial property values of an object at creation using a concise syntax.

58
Q

What are collection initializers?

A

They initialize collections (lists, arrays, dictionaries) with values using a simplified syntax.