In previous articles, Mytour has introduced you to arrays and loops in C#. The next article here will continue to introduce you to classes and objects in C#.
Classes and objects in C#
1. Classes and Objects in C#.
2. Class in C#.
2.1. Declaring a Class in C#.
2.2. Example of a Class in C#.
3. Object in C#.
3.1. Declaring an Object in C#.
3.2. Initializing an Object in C#.
1. Classes and Objects in C#
In C#, a class serves as a blueprint or prototype defined by users from which objects are created. Essentially, a class combines fields and methods (member functions defining operations) into a single unit.
In C#, classes support polymorphism, inheritance, and also provide concepts of derived classes and base classes.
2. Class in C#
2.1. Declaring a Class in C#
Basically, declaring a class in C# involves using the Class keyword, followed by the class identifier (name). Depending on the application requirements, some optional attributes may be used with class declaration. Generally, declaring a class in C# may include the following components:
- Modifier: The class can be public or internal, ... . By default, the class modifier is internal.
- Class Keyword: The Class keyword is used to declare a class type.
- Class Identifier: Variable of class type provided. The identifier (or name) of the class begins with an uppercase letter as convention.
- Base Class or Parent Class: The name of the parent class (super class), if any, precedes the colon (:).
- Interface: List of interfaces separated by commas, if any, precedes the colon (:). A class can implement multiple interfaces. This is optional.
- Body: The body part of the class is enclosed in braces { }.
Constructors in a class are used to initialize new objects. Fields are variables that provide the state of the class and objects, and methods are used to execute the behavior of the class and objects.
2.2. Example of a Class in C#
Below is an illustration of a class in C#:
3. Object in C#
An object is the basic unit of object-oriented programming. A typical C# program creates multiple different objects, interacts with these objects by calling their methods.
An object in C# comprises:
- State: Represented by the object's properties, reflecting its characteristics. Additionally, it mirrors the attributes of an object.
- Behavior: Depicted by the object's methods. Furthermore, it reflects the behavior of an object towards other objects.
In the following example, Dog can be seen as an object, encompassing its identifier, state, and behavior.
Objects in C# correspond to real-world entities. For example, a graphics program may have objects like circles, squares, and menus. Similarly, an online shopping system includes a shopping cart, customers, and online products.
3.1. Object Declaration (also known as Class Initialization) in C#
When an object of a class is created, it is referred to as initialization. All instances share the attributes and behaviors of the class. However, the values of these attributes, i.e., the state, are unique to each object. A class can have multiple different instances.
Here's an example of object declaration in C#:
When we declare variables, it informs the compiler that we will use the name to denote the data type. For primitive variables, this declaration allocates memory for that variable. Thus, for reference variables, the types must be a specific class name.
Example: Dog named tuffy;
If we declare a reference variable (tuffy) as above, its value will remain undefined (null) until an actual object is created and assigned to it. In this case, merely declaring a reference variable does not create an object.
3.2. Object Initialization in C#
The new operator initializes a class by allocating memory for a new object and returning a reference to that memory. Additionally, the new operator invokes the class constructor.
Example of object initialization in C#:
The output result is as follows:
Explanation: This class contains a constructor. We can identify the constructor because its declaration uses the same name as the class and has no return value type.
The C# compiler distinguishes constructors based on the number and type of arguments. The constructor in the Dog class has 4 arguments. The following statement initializes values for 'tuffy', 'papillon', 5, 'white':
Dog instance created with the command:
The execution result of the above command returns as follows:
In this article, Mytour has just introduced you to classes and objects in C#. If you have any questions or need clarification on topics like Nullable in C#, please leave your comments below the article, and Mytour will respond to your inquiries as soon as possible.