- Creating Classes
- Creating Objects
- Using Access Modifiers
- Creating Fields and Using Initializers
- Creating Methods
- Creating Properties
- Read-only Properties
- Creating Constructors
- Creating Structs
- Creating Static Members
- Creating Static Fields
- Creating Static Properties
- Creating Destructors and Handling Garbage Collection
- Overloading Methods
- Overloading Operators
- Creating Namespaces
- In Brief
In Brief
This chapter covered the essentials of C# OOP, starting with creating classes and objects. We saw that access modifiers let you restrict access to the members of classes and structs, encapsulating your data and methods as needed. Here's an overview of the topics we covered:
We can add fields, methods, and properties to classes, and we can initialize fields with initializers. We can use the this keyword in methods, and create not only standard properties, but also read-only and static properties. And we can create constructors and copy constructors to initialize the data in objects.
Static members are declared with the static keyword, and are associated with the class itself, not with objects created from that class. Static fields can store data for the class, and static methods and properties can be called without first instantiating an object.
Method overloading is implemented by providing alternative definitions of the same method with different signatures. Operator overloading uses the operator keyword to let you specify which operator to overload.
When we overloaded the == operator, C# will issue a warning unless we also override the Object.Equals and Object.GetHashCode methods.