Freelance Writing Jobs | Today's Articles | Sign In

 
Browse Sections

C# Operators


In any programming language, operators play an important role in the richness of the language. It allows programmers to do basic arithmetic and comparisons, which is the foundations of many applications today. An application may compute running balances on accounts, keep track of page numbers, site counters or determining if a local hard disk has enough space. All of which will use some form of operator.

What is an operator just exactly? Operators are symbols and notations used in a programming language to express a certain statement. For example if we want to add two variables, we would use the "+" operator. Most programming language, which includes C#, support the basic arithmetic operators.

+ addition
- subtraction
* multiplication
/ division
% modulus


I fondly call these operators Math-Ops for short. For those not familiar with the modulus operator, the operator returns the remainder of a division. Thus an equation such as the following would return 1.

result = 5 % 2;

(1)



Making counters and keeping track of it's value is one of the normal routines a programmer would have to write. It is almost impossible for a programmer to live out an entire programmer career without writing one. Counters normally take the following form:


counter = counter + 1;

(2)



In C#, the above expression can be shortened to:

counter++;

(3)



The double plus operator, or the increment operator, is a favorite among C/C++ programmers because it is very expressive and shortens the source code. In fact, that's how C++ got its name. If we increment C, what do you get? C++ of course. The same applies to subtraction too.

counter = counter - 1;

(4)



The above statement can be expressed as:

counter--;

(5)



There is the concept of post-fix and pre-fix expression for increment operators. This means rather than having the increment operator at the end of a variable, you have it at the front.

++counter;

(6)



If you were to expand the above statement, you'll still end up with equation 2. What the difference between equation 3 and 6? Having the operator in front of the variable

The copyright of the article C# Operators in C# Programming is owned by Jose Aniceto. Permission to republish C# Operators in print or online must be granted by the author in writing.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic