C# Operators

This lesson will instruct you in what operators are used for. No I'm not talking about that lady that chimes in when you dial a disconnected number, but rather instead I'm speaking about how computers think on a logical scale. These are also known as testing expressions.

On the main homepage I listed a few of these examples in the category I briefly mentioned the logical operators such as addition, subtraction, multiplication, etc. Here is a more detailed list of them. A reference to that page is found here.

! - Also known as the "not" operator. So when comparing a variable value if you place an exclamation in front of it such as if (!mymoney) then it reads as 'if not mymoney (!) then do this instead. It serves as something known as a flag and can be used to switch between comparisons. I have used these when I want to turn a value on and off quite often in other programming applications. Here is an example.

if (!mymoney)

Response.Write("I didn't overdraft that account.");

Here is an example of how to activate a switch for the not expression.

introduction to c# programming

& - Also known as the "and" operator.

It is written as the symbol &. It compares other values side by side next to each other. It will result to true from a binary perspective if both statements are true. Be sure not to confuse this with the second and operator below since that will result in true if only one of the comparisons is true. Therefore the above statement is saying if the variable mymoney is not overdrafted (!overdrafted) then write "I didn't overdraft that account.") to the screen. The Response.Write is how ASP.NET writes a statement on the client side. You can also to learn how to write web applications using Visual Studio ASP.NET by clicking on this link

| - Also known as the "OR" operator

This operator will result in a true statement if only one of the comparisons result to true. This is the one I mentioned above that you don't want to confuse with the first & symbol since this is more commonly used during condition evaluations.

&& - Also known as the "AND" operator

I capitalized the word AND to indicate that this expression can be used when you want to be sure that two conditions are equal to each other such as if (myheight == yourheight) then print "We are both the same size in height.". Here is a quick example.

int myheight=6; // 6 feet tall

int yourheight = 6;

if (myheight == yourheight)

Response.Write("We are both the same size in height.");

^ - Also known as the "XOR" operator.

The XOR operator will return the value of true when the comparison of multiple operators equals true. However if each condition results in being true or if they both are false then the value stored here will be false. I remember this value being used in Assembly language back in the old days to compare bits.

|| - Also known as the "OR" operator.

This expression is identical to the original OR (|) above with one exception. The operand on the left must be true, but the second operand on the right won't be true. So it checks for one or the other which is exactly what the name implies. Be sure to check out the page for conditions to see an example of this. You can either click on the navigation panel on the left or click here. You will also find good examples regarding some of the other operators there as well.

Subscribe to the Programmermind newsletter
Privacy Notice: Programmer Mind will not compromise your privacy