Properties and Methods - potatoscript/csharp GitHub Wiki
๐ฅ Properties and Methods in C# ๐ฅ
๐ฅ What Are Properties and Methods? ๐ฅ
In C#, properties and methods are essential parts of a class that define the behavior and characteristics of an object.
- Properties: These define the state or attributes of an object. Think of them as the "variables" inside the object that hold values (like the color of a car or the name of a person).
- Methods: These define the actions or behaviors that an object can perform. Methods are functions that you define within a class to make the object do something (like start the car engine or display the details of a person).
๐ฅ Properties in C# ๐ฅ
Properties are used to store the data (attributes) for a class. They can be public or private, and they can be read-only or read-write. You can think of them like the fields in an object, but with extra features.
Hereโs an example of a class with properties:
public class Car
{
// Properties (Attributes)
public string Make { get; set; } // Make of the car (e.g., Toyota)
public string Model { get; set; } // Model of the car (e.g., Corolla)
public int Year { get; set; } // Year of the car (e.g., 2020)
}
In this example:
- The properties
Make
,Model
, andYear
represent the characteristics of theCar
. - The
{ get; set; }
part allows you to get (read) or set (write) values to these properties.
๐ฅ Example of Using Properties ๐ฅ
Letโs create an object from the Car
class and use the properties to store data:
class Program
{
static void Main()
{
// Create a Car object
Car myCar = new Car();
// Set properties
myCar.Make = "Toyota";
myCar.Model = "Corolla";
myCar.Year = 2020;
// Access and display the properties
Console.WriteLine($"Car Make: {myCar.Make}");
Console.WriteLine($"Car Model: {myCar.Model}");
Console.WriteLine($"Car Year: {myCar.Year}");
}
}
๐ฅ Explanation ๐ฅ
- Set Properties: You use
myCar.Make = "Toyota";
to assign a value to theMake
property. - Get Properties: You use
myCar.Make
to access the value stored in theMake
property.
Properties allow you to easily get or set values for the attributes of an object!
๐ฅ Methods in C# ๐ฅ
Methods are actions or behaviors that you can perform with an object. They are functions defined inside a class and are used to carry out tasks.
Hereโs how a method is defined in a class:
public class Car
{
// Properties
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Method (Behavior)
public void StartEngine()
{
Console.WriteLine("The engine is now running!");
}
// Method to display car details
public void DisplayDetails()
{
Console.WriteLine($"Car Make: {Make}, Model: {Model}, Year: {Year}");
}
}
In this example:
- StartEngine(): This method simulates starting the carโs engine.
- DisplayDetails(): This method prints out the carโs details like
Make
,Model
, andYear
.
๐ฅ Example of Using Methods ๐ฅ
Letโs create an object and use the methods:
class Program
{
static void Main()
{
// Create a Car object
Car myCar = new Car();
// Set properties
myCar.Make = "Toyota";
myCar.Model = "Corolla";
myCar.Year = 2020;
// Calling methods
myCar.StartEngine(); // This will print: "The engine is now running!"
myCar.DisplayDetails(); // This will print: "Car Make: Toyota, Model: Corolla, Year: 2020"
}
}
๐ฅ Explanation ๐ฅ
- Calling a Method: We use
myCar.StartEngine()
to execute theStartEngine
method. - Behavior: When the
StartEngine
method is called, it will print "The engine is now running!". - Method Output: The
DisplayDetails()
method prints out the values of the propertiesMake
,Model
, andYear
.
Methods allow you to define behaviors for your objects and execute tasks that the object can perform!
๐ฅ Getters and Setters (Accessors and Mutators) ๐ฅ
In C#, you can use getters and setters to control access to the properties. These are often referred to as accessors (for getting data) and mutators (for setting data). You can write custom logic inside these methods to control how the property values are accessed or updated.
Hereโs an example:
public class Car
{
private string make; // Private field to store car make
// Getter for Make property
public string GetMake()
{
return make;
}
// Setter for Make property
public void SetMake(string newMake)
{
make = newMake;
}
}
Now you can set and get the value of make
using the SetMake
and GetMake
methods:
class Program
{
static void Main()
{
Car myCar = new Car();
// Using the setter to set the car make
myCar.SetMake("Toyota");
// Using the getter to get the car make
Console.WriteLine($"Car Make: {myCar.GetMake()}");
}
}
๐ฅ Explanation ๐ฅ
- Setter (
SetMake
): TheSetMake
method is used to assign a value to themake
field. - Getter (
GetMake
): TheGetMake
method is used to retrieve the value ofmake
.
Getters and setters give you control over how properties are accessed and modified.
๐ฅ Properties with Validation ๐ฅ
You can also include validation inside a setter to ensure that only valid data is assigned to a property. For example:
public class Car
{
private int year;
// Property with validation
public int Year
{
get { return year; }
set
{
if (value >= 1886) // First car invented in 1886
{
year = value;
}
else
{
Console.WriteLine("Invalid year! Cars did not exist before 1886.");
}
}
}
}
In this example, the setter for Year
ensures that the year is not earlier than 1886, the year the first car was invented.
๐ฅ Explanation ๐ฅ
- Validation: Before setting the
year
property, we check if itโs valid. - Set: If the
value
passed to theYear
property is 1886 or later, theyear
is set; otherwise, an error message is shown.
This way, you can ensure data integrity by validating input data before setting it.