Curriculum
Objects and classes are fundamental concepts in object-oriented programming (OOP). In C#, a class is a blueprint or a template for creating objects, which are instances of the class. Each object created from a class has its own set of properties and methods, and can interact with other objects in a program.
Let’s take an example to understand objects and classes in C#. Consider a class called “Person” that represents a person’s information such as name, age, and gender. Here’s how you would define the class in C#:
public class Person { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } }
The public
keyword means that this class is accessible from anywhere in the program. Inside the class, we define three properties: Name
, Age
, and Gender
. Each property has a get
and set
method, which allows us to get and set the values of these properties.
To create an object of the Person
class, we use the new
keyword and assign it to a variable:
Person person1 = new Person();
This creates a new instance of the Person
class and assigns it to the person1
variable. We can now set the properties of this object:
person1.Name = "John"; person1.Age = 30; person1.Gender = "Male";
We can also create multiple objects of the Person
class:
Person person2 = new Person(); person2.Name = "Jane"; person2.Age = 25; person2.Gender = "Female";
Now we have two Person
objects, person1
and person2
, each with their own set of properties. We can use these objects in our program to perform operations or display information.
This is just a simple example, but classes and objects are used extensively in C# and other object-oriented languages to create complex programs. They allow us to model real-world entities in our code and make our programs more modular and maintainable.
In addition to properties, classes can also have methods, which are functions that operate on the properties of an object. Let’s add a method to the Person
class that returns a string with the person’s information:
public class Person { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } public string GetInfo() { return $"Name: {Name}, Age: {Age}, Gender: {Gender}"; } }
Here we’ve added a GetInfo()
method to the Person
class. This method returns a string that contains the person’s name, age, and gender. We can now call this method on a Person
object:
Console.WriteLine(person1.GetInfo());
This will output “Name: John, Age: 30, Gender: Male” to the console.
Inheritance is another important concept in object-oriented programming. It allows us to create new classes based on existing classes, inheriting their properties and methods. For example, we can create a new class called Student
that inherits from the Person
class:
public class Student : Person { public string StudentId { get; set; } }
Here we’ve defined a new class called Student
that inherits from the Person
class using the :
symbol. This means that the Student
class has all the properties and methods of the Person
class, as well as its own property called StudentId
.
We can now create objects of the Student
class:
Student student1 = new Student(); student1.Name = "Bob"; student1.Age = 20; student1.Gender = "Male"; student1.StudentId = "123456";
Since the Student
class inherits from the Person
class, we can also call the GetInfo()
method on a Student
object:
Console.WriteLine(student1.GetInfo());
This will output “Name: Bob, Age: 20, Gender: Male” to the console, since the GetInfo()
method is defined in the Person
class.
In conclusion, objects and classes are essential concepts in C# and object-oriented programming. They allow us to create complex programs by modeling real-world entities and making our code more modular and maintainable.