letβs go through members in C# clearly.
π 1. What Are Members?
In C#, the term member refers to everything you can define inside a class, struct, or interface.
Members define the structure (data) and behavior (actions) of a type.
π 2. Types of Members
Hereβs a breakdown:
1. Fields
- Variables declared inside a class/struct
public class Person
{
public string name; // field
}
2. Properties
- Encapsulated access to fields
public string Name { get; set; } // property
3. Methods
- Functions inside a class
public void Greet() { Console.WriteLine("Hello!"); }
4. Constructors & Destructors
- Special methods for initialization and cleanup
public Person(string n) { name = n; } // constructor
~Person() { /* cleanup code */ } // destructor
5. Events
- For signaling between objects (based on delegates)
public event EventHandler OnNameChanged;
6. Indexers
- Allow object access via
[]
public string this[int index] { get { return data[index]; } set { data[index] = value; } }
7. Operators
- Overloaded operators
public static Person operator +(Person a, Person b) { return new Person(a.name + b.name); }
8. Nested Types
- Classes, structs, enums, interfaces inside other classes
public class Outer
{
public class Inner { }
}
π 3. Access Modifiers for Members
-
public
β accessible everywhere -
private
β accessible only within the class -
protected
β accessible in the class + subclasses -
internal
β accessible within the same assembly
π 4. Quick Example With Multiple Members
public class Car
{
// Field
private string brand;
// Property
public string Brand
{
get { return brand; }
set { brand = value; }
}
// Constructor
public Car(string brand)
{
this.brand = brand;
}
// Method
public void Drive()
{
Console.WriteLine($"{brand} is driving!");
}
// Event
public event EventHandler EngineStarted;
// Indexer
private string[] parts = { "Wheel", "Engine", "Seat" };
public string this[int index] => parts[index];
}
π 5. Analogy
-
Think of a class as a toolbox.
-
The members are the tools inside (variables, methods, properties, events, etc.).
β
In summary:
Members in C# = everything you can define inside a class, struct, or interface (fields, properties, methods, constructors, events, indexers, nested types, etc.).