Friday, April 19, 2024 22:17

Table of contents >> Introduction > Other operators

Other operators

Aside of the operators I have enumerated so far, there are a few other operators that are either too simple, too rarely used or too unimportant to create additional posts for them.

The . (dot) operator. We’ve used it quite a lot so far (remember Console.WriteLine?). There is no “basic” explanation for this one, and since it deals with stuff we haven’t learned yet, it might be a bit confusing to fully understand what it does. However, the academic definition is that it is used to access member fields or methods of a class or object. For instance, C# defines a class called DateTime, which contains a property (a field member) called NowIn order to access it, we need to use the dot operator:

In the above example, we use the dot operator twice, to access the WriteLine() method of the Console class, and the Now property of DateTime class.

The [] (square brackets) operator. This operator is mainly used to access elements of an array. We haven’t learned arrays yet, but you may imagine an array as being a number of values of same type, grouped in the same variable. As a real life example, imagine a building being an array variable, and the apartments inside it, the array members (or values). If you want to find a specific address, you need to specify the building name and the apartment number inside it. Lets do this in code:

So, we declared an integer array variable called building (which can store one or more integers), and we assigned it three integer values (our “apartments”). In order to access a specific apartment of the building, we used the [] operator: building[0], building[1], etc. Note that in C#, arrays have 0 based indexes! This means that the index of the first element of an array is 0, NOT 1. Consequently, if an array has 7 elements, the index of the last element is 6, not 7! If you declare an array of 3 elements and you try to access array[3], you will get an IndexOutOfRange exception and your program will crash, because that array only contains elements at 0, 1 and 2 indexes.

The [] operator can also be used to access characters inside a string, like this:

which will output “h”. You can view a string as an array of characters, and the character residing at index 2 inside our string is letter “h”.

The () (brackets) operator. As we already explained, this operator can be used to change the order of execution inside an expression, arithmetical or otherwise.

Operator new. The new operator can be used to instantiate new objects. We will cover it in a future lesson.

Operator is. This operator is used to check if an object is compatible with a certain type or not (in order to check that object’s type).

Operator ??.  You can think of this operator as being somehow similar to the ?: operator, with the only difference that it does not have a condition which we are specifying. Instead, being placed between two operands, it returns the left operand if its value is not null, or the right one, otherwise. Short example:

Operator as. It is used for type conversion as we will learn later, and in its case, invalid conversion returns null.

Type conversion operator. Also used for type conversion, but generates an exception if the conversion is invalid.

The concepts explained in this lesson are also shown visually as part of the following video:

Tags: ,

Leave a Reply



Follow the white rabbit