Classes and members (C#) Flashcards
What does object orientated programming (OOP) mean?
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.
Which concepts does OOP emphasize?
- Encapsulation.
- Inheritance.
- Polymorphism.
- Abstraction
What is the difference between an object-oriented language and a non-object-oriented language?
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.
Why is C# object-oriented?
It supports the fundamental principles of OOP.
It provides features like classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
What are classes in C#?
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.
What are structs in C#?
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.
What are records in C#?
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 do we use records in C#?
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
What are abstract classes in C#?
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.
What are abstract members C#?
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.
What does the keyword “static” mean/do?
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.
What is evolving classes in C#?
?
What are partial classes in C# and why do we use them?
- 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.
- Partial classes are often used in code generation scenarios or when multiple developers need to work on different parts of a class simultaneously.
What does the keyword “partial” mean/do?
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.
What are the 4 object-oriented features?
- Encapsulation.
- Abstraction.
- Inheritance.
- Polymorphism.
What is encapsulation and why use it?
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.
What is abstraction and why use it?
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.
What is inheritance and why use it?
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.
What does specialization and generalization mean in relation to inheritance?
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.
What does it mean that inheritance is transitive?
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 does constructors work in inheritance?
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.
What is “preventing” inheritance and what does the keyword “sealed” mean/do?
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.
What is polymorphism and why use it?
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.