Chapter 5: Building Your Own Types with Object-Oriented Programming [Flashcarder]
What are the primary types of members that an object-oriented type in C# can have?
Fields to store data and methods to perform actions.
Which OOP concepts are used to combine related data and actions in a type?
Encapsulation
What is aggregation in object-oriented programming?
Aggregation refers to combining objects to form a new component. For example, a Person and a Car object can be aggregated when the Person becomes the Driver.
What is the difference between composition and aggregation in OOP?
Composition defines what an object is made of (e.g., a Car is composed of Wheels and an Engine), while aggregation refers to objects that can work together (e.g., a Person can drive a Car, but they are separate entities).
How is inheritance used in C#?
Inheritance allows a subclass to derive from a base class, inheriting all its functionality. The derived class can also extend or override certain functionality.
What is abstraction in OOP, and how is it implemented in C#?
Abstraction captures the core idea of an object and hides the details. It is implemented using abstract classes and interfaces.
What keyword is used to define an abstract class in C#?
The abstract keyword.
What is polymorphism in OOP?
Polymorphism allows a derived class to override an inherited method to provide custom behavior.
What are the keywords used in C# to define an object type?
class, record, and struct.
What C# keyword is used to restrict access to a class’s internal data?
private
What is the purpose of class library assemblies in .NET?
Class library assemblies group types together into easily deployable units, such as DLL files, to make code reusable across multiple projects.
How do you create a new class library in .NET using Visual Studio?
Select “Class Library” as the project template, name the project (e.g., PacktLibraryNetStandard2), and specify the solution folder (e.g., Chapter05).
What is the default target framework for class libraries created with the .NET 8 SDK?
The default target framework is .NET 8.0.
How can you modify a class library project to target .NET Standard 2.0 and use the C# 12 compiler?
Modify the <TargetFramework> to netstandard2.0, add <LangVersion>12</LangVersion>, and import the System.Console class statically for all C# files.</TargetFramework>
Why can’t all modern C# features be used in a .NET Standard 2.0 class library?
Some modern C# features, like default implementations in interfaces (C# 8) or the required keyword (C# 11), require newer .NET runtimes, such as .NET Standard 2.1 or .NET 7.
What is the recommended practice for supporting both modern and legacy .NET platforms in class libraries?
For modern features, use a .NET 8 class library. To support legacy platforms like .NET Framework and Xamarin, create a .NET Standard 2.0 class library and override the default C# 7 compiler to a newer version.
How do you compile a .NET class library in Visual Studio 2022?
Navigate to Build | Build <ProjectName> (e.g., Build PacktLibraryNetStandard2).</ProjectName>
How do you traditionally define a type such as a class in a namespace in C#?
Types are defined within curly braces inside a namespace block. For example:
namespace Packt.Shared { public class Person { } }
What is a file-scoped namespace in C# and how is it different from traditional namespaces?
A file-scoped namespace simplifies code by ending the namespace declaration with a semicolon and removing the curly braces, allowing all types in the file to be part of the namespace without indentation. Example:
namespace Packt.Shared; public class Person { }
Why is it considered good practice to put each type in its own code file or group types in the same namespace in a file?
It allows for the use of file-scoped namespaces, simplifying the structure and making the code easier to manage.
Why should classes be placed in a logically named namespace?
Logical namespaces, such as domain-specific ones (e.g., System.Numerics), help clarify the purpose of the types. For generic types without a domain, a more general namespace like Packt.Shared can be used.
What is the default access modifier for a class in C# if none is specified?
The default access modifier for a class is internal, meaning it is accessible only within its own assembly.
What does the public access modifier do when applied to a class in C#?
It allows the class to be accessible from other assemblies, making it available for use outside its own assembly.
What is the use of the file access modifier introduced in .NET 7?
The file access modifier restricts the visibility of a type to only the code file in which it is defined. This is rarely used but can be helpful in scenarios like source generators.