Object-Oriented Programming
Object-oriented programming (OOP) is a programming paradigm that organizes code into objects, which are instances of classes. In C#, every piece of code you write is inside a class.
Classes and Objects
A class is a blueprint for creating objects. It defines the properties and behaviors that an object of that class will have. For example, you can have a Person
class that represents a person with properties like Name
and Age
.
1class Person
2{
3 public string Name { get; set; }
4 public int Age { get; set; }
5}
To create an object of a class, you use the new
keyword followed by the class name and parentheses.
1Person person = new Person();
In this example, person
is an object of the Person
class.
Properties
Properties are used to encapsulate data and provide a way to access and modify it. They define the characteristics of an object. In the Person
class example above, Name
and Age
are properties.
Constructors
A constructor is a special method that is called when an object is created. It initializes the object's state and sets its initial values. In the Person
class example, there is a constructor that takes two parameters (name
and age
) and sets the Name
and Age
properties of the object.
Methods
Methods are functions that define the behavior of an object. They can perform specific actions and can be called on objects of the class. In the Person
class example, there is a Greet
method that prints a greeting message using the Name
and Age
properties.
Example
Here's an example that demonstrates the concepts of classes, objects, properties, constructors, and methods in C#:
1class Person
2{
3 // Properties
4 public string Name { get; set; }
5 public int Age { get; set; }
6
7 // Constructor
8 public Person(string name, int age)
9 {
10 Name = name;
11 Age = age;
12 }
13
14 // Method
15 public void Greet()
16 {
17 Console.WriteLine($"Hello, my name is {Name} and I'm {Age} years old.");
18 }
19}
20
21// Create an instance of Person
22Person person = new Person("John", 25);
23
24// Call the Greet method
25person.Greet();
In this example, we define a Person
class with Name
and Age
properties, a constructor that initializes the properties, and a Greet
method that prints a greeting message. We then create an instance of the Person
class named person
with the name "John" and age 25, and call the Greet
method to print the greeting message.
Object-oriented programming is a powerful concept that allows you to create reusable and modular code. It provides a way to model real-world entities and interactions in your programs.
Keep coding and exploring the world of object-oriented programming!
xxxxxxxxxx
class Person
{
// Properties
public string Name { get; set; }
public int Age { get; set; }
// Constructor
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Method
public void Greet()
{
Console.WriteLine($"Hello, my name is {Name} and I'm {Age} years old.");
}
}
// Create an instance of Person
Person person = new Person("John", 25);
// Call the Greet method
person.Greet();