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, and Year represent the characteristics of the Car.
  • 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 the Make property.
  • Get Properties: You use myCar.Make to access the value stored in the Make 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, and Year.

๐Ÿฅ” 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 the StartEngine 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 properties Make, Model, and Year.

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): The SetMake method is used to assign a value to the make field.
  • Getter (GetMake): The GetMake method is used to retrieve the value of make.

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 the Year property is 1886 or later, the year is set; otherwise, an error message is shown.

This way, you can ensure data integrity by validating input data before setting it.