Lesson 3

Learning About Methods

Learning About Methods

Welcome back! Now that you know how to define classes, attributes, and objects in C#, it's time to take the next step in our journey. In this lesson, we'll focus on methods, which are essential for adding behavior to your classes. Methods allow your objects to perform actions, making your programs dynamic and interactive.

What You'll Learn

In this lesson, we'll dive into methods: what they are, how to define them, and how to use them. A method in a class is a block of code that performs a specific task. In other words, it's an action that your objects can take. You'll learn how to add methods to your classes, and how to call these methods on objects.

Here's a quick example to get us started:

C#
1class Spaceship 2{ 3 // Public attribute 4 public string? name; 5 6 // Method to launch the spaceship 7 public void Launch() 8 { 9 Console.WriteLine(name + " is launching into space!"); 10 } 11} 12 13class Program 14{ 15 static void Main() 16 { 17 // Create an object of the Spaceship class 18 Spaceship myShip = new Spaceship(); 19 20 // Set the name attribute 21 myShip.name = "Voyager-1"; 22 23 // Call the Launch method 24 myShip.Launch(); 25 } 26}

In this example, the Launch method is defined in the Spaceship class. When you call myShip.Launch(), the program will print a message indicating that the spaceship is launching.

How to Define a Method in a Class

To define a method in a class, specify its access modifier, return type, name, and parameters (if any). Unlike normal functions, methods are defined within the scope of a class and can access the class's attributes and other methods. Methods often use the public keyword, making them accessible from outside the class.

Here's an extracted piece of code from the example:

C#
1class Spaceship 2{ 3 // Public attribute 4 public string? name; 5 6 // Method to launch the spaceship 7 public void Launch() 8 { 9 Console.WriteLine(name + " is launching into space!"); 10 } 11}

In this example:

  • public is the access modifier, making the method accessible from outside the class.
  • void is the return type, indicating that the method does not return a value.
  • Launch is the method name.

Unlike normal functions, methods are tied to the object's state and can interact with the object's attributes. This encapsulates behavior within the class, making your code more organized and modular.

How to Access a Method

To access a method, you need to have an instance of the class. Once you have the object, you can call its methods using the dot (.) operator. Here's how:

C#
1myShip.Launch();

In this code:

  • myShip is an object of the Spaceship class.
  • Launch() is the method being called on the myShip object.

Ensure the object is instantiated and the necessary attributes are set before calling the method. This allows the method to execute using the object's state.

Why It Matters

Methods are crucial in Object-Oriented Programming because they define how objects behave. They enable you to encapsulate functionality within your classes, making your code more modular and reusable. By using methods, you can break down complex tasks into smaller, manageable chunks, improving both the readability and maintainability of your code.

Imagine programming without methods — you would have to write the same logic repeatedly for each object, making your code repetitive and harder to manage. With methods, you write the logic once and call it whenever needed. This ability to encapsulate behavior is what makes methods powerful tools in your programming arsenal.

Ready to make your classes come to life? Let's dive into the practice section and start adding some methods to your classes together!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.