Greetings! Today, we're revisiting C# classes, the core building block of Object-Oriented Programming (OOP) in C#. Classes play a pivotal role in structuring code to model real-world entities and behaviors. Through hands-on examples, we'll revisit the fundamental concepts of C# classes, including their structure, properties, and methods. This session aims to enhance your understanding of how classes facilitate better code organization and reusability.
Let's begin with a refresher on C# classes. Essential to OOP, C# classes bundle relevant data and functions into compact units called objects. Consider a video game character, which is a typical example of a class instance, with specific properties (such as Health
and Strength
) and methods (such as Attack
).
In the code above, the GameCharacter
class has properties Name
, Health
, and Strength
. Each of these properties includes a getter, which allows the retrieval of the corresponding private field values—name
, health
, and strength
. Getters provide a way to access these private fields from outside the class, maintaining encapsulation while allowing read-only access to these properties.
C# classes facilitate the grouping of associated code elements, simplifying their management. Now, let's go through it step-by-step to better understand how the above example works.
A C# class serves as a blueprint consisting of properties and methods. While properties represent data relevant to a class instance, methods are actions or functions that manipulate this data. Each class includes a constructor
function, which is used to define class properties. A constructor is a special method that is called when an object of the class is created.
An essential keyword within these methods is this
, which represents the class instance. In the constructor, this.name
refers to the class property name
, while name
refers to the parameter passed to the constructor. In object-oriented programming, this
is needed to access the class's properties and methods. When a new class instance is created, C# automatically passes it to the this
parameter to access individual instance properties and methods using the this
keyword. This mechanism allows each object to keep track of its own state and behaviors.
When creating an instance of the class, parameters are passed to the constructor, and a new instance of the class is created, as demonstrated in GameCharacter character = new GameCharacter("Hero", 100, 20);
.
Properties in C# classes hold data associated with each class instance. For example, in our GameCharacter
class, Name
, Health
, and Strength
are properties.
In the code block, we create a GameCharacter
object and store it in the character
variable with the name 'Hero', health set to 100, and strength set to 20. We can then access its name by using character.Name
, which will return 'Hero'. Similarly, character.Health
returns 100, and character.Strength
returns 20.
Properties differentiate one class instance from another and store the state of the instance.
A class also contains methods — actions or functions that manipulate the data in the class. For example, the Attack
method in our GameCharacter
class simulates an attack by one game character on another.
This code demonstrates object interaction using the Attack
method. Initially, character2
has a health of 80. character1
invokes the Attack
method on character2
, reducing the health of character2
by character1
's strength (20). After the attack, character2
's health decreases to 60.
To deepen our understanding of C# classes, let's explore another example where we build a basic BankAccount
class. This class will demonstrate how we can model real-world entities using object-oriented programming by defining properties like the account holder's name and balance, and methods for depositing and withdrawing money.
In the BankAccount
class, the properties HolderName
and Balance
include getters that enable access to the private fields holderName
and balance
. These getters allow external code to retrieve the account holder's name and account balance while maintaining data encapsulation, as the fields are not directly accessible from outside the class.
This example further illustrates how classes effectively encapsulate data (properties) and functionalities (methods), enabling us to mimic real-life scenarios. Here, the BankAccount
class allows the creation of objects representing bank accounts, emphasizing the powerful organizational benefits of using classes in C#.
Great work revisiting C# classes, their properties, and methods. C# classes help organize your code, improving its readability and manageability. Now, test your understanding with exercise problems to solidify your newly refreshed knowledge. Happy coding!
