Assign Values to Your Variables with the .Net = Operator

microsoft access 2010 tutorialOperators are symbols that tell us when to perform some set of manipulations on a collection of data. More than simply mathematical or logical functions, we use operators for a wide variety of activities from computer programming to everyday tasks. Even in programming, we have enough operators to perform any task our hearts desire and then some. However, they would all be worthless without assignment operators. In .Net, the assignment operator is represented by the .Net = operator and I hope this post will show you how powerful it is.

Find out how VB.Net uses the = operator at Udemy.

The .Net = Operator

Operators take data and transform it. While we usually only consider the common mathematical symbols as operators, they can take any form. Every programming class method you make that takes arguments and returns some value is an operator by definition. The assignment operator is no different. It takes some data and stores it some memory location.

Before we continue, I have to break something to you. There is no .Net = operator, or any other basic operator. All the basic operators for addition, subtractions, assignment, and all other basic operations come from the programming language you use to access the .Net framework. While most languages use the = operator for assignment like I will detail below, some languages give it other meanings such as the equality operator in early versions of Visual Basic.

Using the .Net = Operator

The .Net = operator you are probably most familiar with is the C# = operator. In C#, = works like it does in any other C-based programming language. Referred to as a binary operator, you use it by placing your expression on the right side of the = sign and your storage variable on the left. Your .Net = operator statement should look like this:

myVariable = 34 + 4;
myString = “Operators rock!”;
area = System.Math.PI() * radius * radius;
quote = SalesPrice(productID, quantity);
int productID = 0; // You can even use the assignment operator in declaration statements!

I want to stress that you must have your storage variable on the left side of the .Net = operator. You can place any C# or .Net expression or statement on the right side including literal data and other variables, but you have to have a variable on the left or the compiler will return an error.

The .Net = operator works by evaluating the statement to its right, and then storing the result in the memory space represented by the variable on its left. You can even use it to initiate class objects. Since C# is a strongly typed language, your storage variable must be of the same data type as the result of the statement on the right side of the operator.

Brush up your C# skills at Udemy

Don’t confuse the .Net = operator with the == operator.

With all the power the = operator has, you should never use it to test equality. The C# equality operator is ==, and not =. This is one of the common syntax errors programmers make, and it is one of the most difficult to detect. The compiler won’t tell you that you made the error. Assignment statements are valid C# statements, and that’s all the comparison statements look for. Functionally, they have the value of the expression on their rights. If the expression results in a non-zero number, the conditional will consider it as true. If zero, assignment operators render as false. If this was you intended outcome, you have nothing to worry about. If not, your programs will act weird, and it might take you some time to figure out why. Therefore, you should always make sure that you used the proper operator for your statement.

Understand how to use all the operators in C# at Udemy.

Other Assignment Operators

Another power of the .Net = operator is that you can put your storage variable on the right side of the = sigh as well as the left. This will effectively update the values stored in the variable. When you use this features, you should put the variable right next to the operator. It’s not required, but it will make your code more readable.

quote = quote + new_price;
inventory = inventory – items_sold;

The above feature is so useful and common practice that C# comes with other assignment operators that combine the = operator with the mathematical operator in the expression. These new operators take the place of the operators and your variable on the right side of the assignment statement. For instance, we can write the last two statements as:

quote += new_price;
inventory -= item_sold;

I have includes all the .Net assignment operators in the table below

OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B will assign value of A + B into C
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C – A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandC *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operandC /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandC %= A is equivalent to C = C % A
<<=Left shift AND assignment operatorC <<= 2 is same as C = C << 2
>>=Right shift AND assignment operatorC >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operatorC &= 2 is same as C = C & 2
^=bitwise exclusive OR and assignment operatorC ^= 2 is same as C = C ^ 2
|=bitwise inclusive OR and assignment operatorC |= 2 is same as C = C | 2

 

The .Net = operator serves to assign values to your variables. This powerful feature makes all programming languages as useful as they are. Without the assignment operator, you would have to use data on the fly as you would have no way to store it for later.  That alone makes the .Net = operator the most powerful operator in programming.