Classes, Methods and Properties in C # – Programming, Pseudocode Example, C# Programming Example

Classes, Methods and Properties in C #

The concept of class is the basis of any object-oriented programming language and has the power to define the characteristics of a set of objects that have properties and perform the same actions. In fact, it represents all objects belonging to a certain class.

For example, think of cars: each car is different from another but they all have four wheels perform the action of directing or braking. These common elements make it a unique concept. A class.

More concretely, we can say that a class is a collection of variables, methods and properties. Also in this case, C # uses the syntax of Java to build a class, called class.

Sometimes, a class is also identified as a “data type“, in fact it carries both the representation of the data and the operations of the latter, in fact the class concept is broader.


Objects and Builders

We can consider instances of a class, that is, objects that realize the concept of class, as variables defined by a certain type of data.

For each instantiated class, an object is created and a class constructor is called. The constructor, like Java and C ++, has the same class name and returns no value (in our example, the Person public method). In VB .NET, however, the constructor is defined in a method declared as Sub New.

The constructor is used to define the properties that the class must have when it is created: in our example, when creating an instance of the Person class, its variables mFirstName  and mName take the values specified in the constructor argument .


The methods, what are they and how to declare them

Methods provide access to objects to simulate, perform actions, or verify behaviors. More pragmatically these are routines (functions or procedures), let’s see how to declare them.

The methods of a class can be defined as public, private, protected, or internal (“Friend” in Visual Basic .NET): These keywords are usually called access modifiers.

With the public, a routine becomes accessible to all instances of the class. private, on the other hand, prevents the routine from being visible outside the class in which it is defined.

If you do not specify the access modifier, private is the default. For example, always using our Person class:

By declaring a method of the Person class as protected, it will only be visible if we define the classes inherited by Person but it can not be used in the code where we put objects in this class. Heredity will be treated in the next lessons.

In practice, protected looks like private, with the difference that the method is also visible for classes inherited from the parent. Finally, what is declared as internal is only visible in the entire application. For the assembly concept, refer to the lessons dedicated to Visual Studio.

After the method access modifier, you must specify the data type returned in the declaration.

You can specify any type of data: int, string, byte, and so on. You can also specify the name of a class because remember that a class is a type. This type of method corresponds to the VB Function.

To return the value, you must use the return keyword, followed by any expression of the same type as the return value of the method. This statement also causes the method to exit. For example :

In C #, a method that returns a value must necessarily contain the return keyword, otherwise, during compilation, you will get an error.

However, the Visual Basic features may not have the return: in this case, the value of return is the default value for the particular data type.

Methods that do not return a value, such as PrintMessage () and Support () in the example, have a type void; they correspond to Visual Basic’s Sub. You can always use the return word in a void method, which in this case only serves to exit the procedure: in this case, return should not be followed by an expression.



A class can also contain other types of methods, called properties. These are special routines that allow you to read and / or define the values of certain properties of a class. For example, add the following property to the Person class:

This allows to read or (re) write variable mFirstName, otherwise it would not be visible outside the class. The get must return the variable associated with the property. Thus, as indicated above, the return instruction must be provided.

However, the set is executed when you change the value of the property; the value keyword contains the new value to assign. The use of properties is intuitive:

Similarly, you can add a Name property.

Surely you are wondering why you should use the properties when you could just make public the variables mFirstName and mName. It’s best to have a private class member only accessible through the properties when you need to control the values assigned to the members themselves.

For example, suppose we want to avoid entering a name or empty username: if we set the mFirstName and mName variables as public, this will not be possible.

In fact, you have free access to variables that can be set on any valid string (including, therefore, the empty string). To resolve this problem, simply set the members as private and modify the FirstName property as follows:

Similarly, the Name property must also be redefined.

string.Empty is a read-only field that represents the empty string. If FirstName or Name is set to the empty string, this code sets them as No First Name and No Name, respectively.

We note that to obtain consistent behavior of the entire class, the constructor must also be modified to verify the arguments.

It is good to define properties that allow you to modify the values ​​of variables used by a class, in order to have more control over the specified data rather than giving you free access to the variables themselves.

Properties can also be read-only if they only have the get, or write-only, if they only have the set.

To achieve similar behavior in Java or C ++, you must define two separate methods, called getter (to retrieve value) and setter (to set it) methods.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.