Lesson 2
Defining Classes, Attributes and Objects
Defining Classes, Attributes, and Objects

Welcome back! Now that you understand the basics of the Program class and the Main function, it's time to take the next step in your C# journey. In this lesson, we will focus on defining our own classes, attributes, and objects, which are core concepts of Object-Oriented Programming (OOP). C# is an OOP language, and this knowledge is crucial as it forms the basis for creating structured and reusable code.

Understanding OOP and its Key Concepts

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and programs. It makes your code more modular, reusable, and easy to maintain. Here's a quick breakdown of its key concepts:

  • Class: Think of a class as a blueprint for creating objects. It defines a type and outlines what functionalities that type has.
  • Attribute: These are variables defined in a class that hold data about the objects created from the class. For example, an attribute of a Spaceship class might be its name.
  • Object: An object is an instance of a class. If the class is a blueprint, the object is the actual creation based on that blueprint. Each object can have different attribute values. For instance, a Spaceship object could have the name "Voyager-1".
What You'll Learn

In this lesson, you'll learn how to define a class, add attributes, and create objects in C#. Classes act as blueprints for objects, attributes store the data for these objects, and objects are instances of these classes.

Here's a simple code example to illustrate this:

C#
1class Spaceship 2{ 3 // Attribute to store the name, initialized to null 4 public string? name; 5} 6 7class Program 8{ 9 static void Main() 10 { 11 // Create an object of the Spaceship class 12 Spaceship myShip = new Spaceship(); 13 14 // Set the name attribute of the spaceship object 15 myShip.name = "Voyager-1"; 16 17 // Print the name of the spaceship to the console 18 Console.WriteLine(myShip.name); 19 } 20}

By the end of this lesson, you'll know how to define a class, add attributes, and create objects just like we did for the Spaceship class.

Defining Attributes in a Class

An attribute in a class is defined by specifying its data type and name. Let's break down our example to better understand how to define an attribute in a class.

C#
1public string? name;

In this code:

  • public keyword is used to declare an attribute that can be accessed from outside the class.
  • string? means that the name attribute can hold either a string value or be null. The ? symbol in C# indicates that the type is nullable, meaning the attribute can be set to a string or left as null by default.
  • name is the attribute's name, which can be used to store information about the object.

By making an attribute public and possibly nullable (?), you allow for more flexible and accessible data handling within your class instances.

Creating an Object of a Class

Creating an object of a class allows you to use the blueprint defined by the class to make an actual instance with specific attribute values. A class can have as many objects as you want, each with its own set of attribute values.

In C#, you create an object using the new keyword, followed by the class name and parentheses. Here's how:

C#
1Spaceship myShip = new Spaceship();

This line of code creates a new instance of the Spaceship class and stores it in the myShip variable. You can now access and modify the attributes of myShip just like we did in the example by setting the name attribute to "Voyager-1".

Accessing an Attribute

Once you've defined attributes in a class and created an object of that class, you can access and modify these attributes using the dot (.) operator.

This allows you to interact with the object's data and update it as needed:

C#
1myShip.name = "Voyager-1"; // Setting the attribute 2Console.WriteLine(myShip.name); // Accessing the attribute

This demonstrates how to interact with the attributes of an object, allowing you to store and retrieve the specific data for each instance of your class.

Why It Matters

Defining classes, attributes, and objects is fundamental in C#. By mastering this, you can create more complex and organized software. Classes help you encapsulate data and functions, making your code modular and easier to manage. Attributes store the data for each instance of a class, and objects make it possible to create multiple instances of a class, each with its own state.

Understanding how to define classes, add attributes, and create objects will open the door to more advanced OOP concepts, such as inheritance, encapsulation, and polymorphism. These are essential skills for building robust and scalable applications.

Ready to make your code more powerful and organized? Let's move on to the practice section and start defining some classes, attributes, and objects together!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.