Examine class members

Completed

Object-oriented programming (OOP) is a programming paradigm that uses objects to model real-world entities. In OOP, classes are the blueprint for creating objects and define the data and behavior of those objects.

Classes use fields to define the data of an object, properties to control access to fields, and methods to define the behavior of an object.

Although properties and methods often get the most attention, classes include other members that help to define their structure and behavior. The following table lists the members of a class:

Member Description
Fields Fields are variables declared at class scope. A field may be a built-in numeric type or an instance of another class. For example, a calendar class may have a field that contains the current date.
Constants Constants are fields whose value is set at compile time and can't be changed.
Properties Properties are methods on a class that are accessed as if they were fields on that class. A property can provide protection for a class field to keep it from being changed without the knowledge of the object.
Methods Methods define the actions that a class can perform. Methods can take parameters that provide input data, and can return output data through parameters. Methods can also return a value directly, without using a parameter.
Events Events provide notifications about occurrences, such as button clicks or the successful completion of a method, to other objects. Events are defined and triggered by using delegates.
Operators Overloaded operators are considered type members. When you overload an operator, you define it as a public static method in a type.
Indexers Indexers enable an object to be indexed in a manner similar to arrays.
Constructors Constructors are methods that are called when the object is first created. They're often used to initialize the data of an object.
Finalizers Finalizers are rarely used in C#. They're methods that are called by the runtime execution engine when the object is about to be removed from memory. They're used to make sure that any resources which must be released are handled appropriately.
Nested Types Nested types are types declared within another type. Nested types are often used to describe objects that are used only by the types that contain them.

Fields, properties, methods, and constructors in a class definition

Although a class definition can include all of the members listed in the table, the most common are fields, properties, methods, and constructors. These members form the core structure of most classes in C#, providing the necessary data storage, access mechanisms, behavior definitions, and initialization logic.

The following code snippet illustrates how common class members can be used to define and implement a class in C#:


public class Person
{
    // Fields
    private string name;
    private int age;

    // Properties
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    // Constructors
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    // Methods
    public void Display()
    {
        Console.WriteLine($"Name: {Name}, Age: {Age}");
    }
}

class Program
{
    static void Main()
    {
        Person person = new Person("PersonName", 30);
        person.Display();
    }
}

This example uses the following member types:

  • Fields: The Person class has two fields, name and age, which are private.
  • Properties: The Person class has two properties, Name and Age, which provide access to the private fields. The properties use the get and set accessors to read and write the values of the fields.
  • Constructors: The Person class has a constructor that initializes the name and age fields when a new Person object is created.
  • Methods: The Person class has a Display method that writes the name and age of the person to the console.

The Program class contains the Main method, which serves as the entry point of the application. The Main method creates a new Person object, initializes it with the name "John" and age 30, and then calls the person.Display method to write the person's information to the console.