Understanding C# Class Members

Class members in C# include fields, methods, properties, constructors, and events. They define the data and behavior of a class.

Key Topics

Fields

Fields are variables declared directly in a class. They represent the state or data of a class.

Example: Using Fields

public class Car
{
    // Fields
    public string Make;
    public string Model;
    public int Year;
}

class Program
{
    static void Main(string[] args)
    {
        // Create a new Car object
        Car car = new Car();
        
        // Assign values to fields
        car.Make = "Toyota";
        car.Model = "Corolla";
        car.Year = 2020;
        
        // Print field values
        Console.WriteLine($"Make: {car.Make}, Model: {car.Model}, Year: {car.Year}");
    }
}

Output:

Make: Toyota, Model: Corolla, Year: 2020
                        

Explanation: The Car class has three fields: Make, Model, and Year. In the Main method, we create an instance of Car and assign values to these fields, then print them.

Methods

Methods define the behavior of a class. They are functions that can perform actions using the class's data.

Example: Using Methods

public class Car
{
    // Fields
    public string Make;
    public string Model;
    public int Year;

    // Method
    public void StartEngine()
    {
        Console.WriteLine("Engine started.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create a new Car object
        Car car = new Car();
        
        // Call the StartEngine method
        car.StartEngine();
    }
}

Output:

Engine started.
                        

Explanation: The Car class includes a method StartEngine() that prints a message. In the Main method, we create an instance of Car and call the StartEngine() method.

Properties

Properties provide a flexible mechanism to read, write, or compute the values of private fields. They are accessors that can be used like public data members but contain code like methods.

Example: Using Properties

public class Car
{
    // Private field
    private string _color;

    // Property to get and set _color
    public string Color
    {
        get { return _color; }
        set { _color = value; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create a new Car object
        Car car = new Car();
        
        // Set the Color property
        car.Color = "Red";
        
        // Get and print the Color property
        Console.WriteLine($"Car Color: {car.Color}");
    }
}

Output:

Car Color: Red
                        

Explanation: The Car class has a private field _color and a public property Color to get and set the value of _color. In the Main method, we set the color using the property and then print it.

Access Modifiers

Access modifiers control the visibility of class members. The most common are public, private, protected, and internal.

Example: Applying Access Modifiers

public class Car
{
    // Private fields
    private string make;
    private string model;
    private int year;

    // Public method to set the make
    public void SetMake(string carMake)
    {
        make = carMake;
    }

    // Public method to get the make
    public string GetMake()
    {
        return make;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create a new Car object
        Car car = new Car();
        
        // Set the make using the public method
        car.SetMake("Honda");
        
        // Get the make and print it
        Console.WriteLine($"Make: {car.GetMake()}");
    }
}

Output:

Make: Honda
                        

Explanation: The fields make, model, and year are declared as private. We use the public methods SetMake() and GetMake() to access the private field make. Direct access to make from outside the class is not allowed due to the private access modifier.

Key Takeaways

  • Fields: Variables that hold data within a class.
  • Methods: Functions that define class behavior.
  • Properties: Provide controlled access to private fields.
  • Access Modifiers: Keywords that define the accessibility of class members.
  • Encapsulation: The concept of restricting access to certain components of an object.