Demonstrates compound assignment operators : Operators « Language Basics « C# / C Sharp

Demonstrates compound assignment operators /*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
//  Assign.cs - Demonstrates compound assignment operators
//
//  Compile this program with the following command line:
//           C:>csc Assign.cs
//
namespace nsAssignment
{
using System;

public class Assign
{
static public void Main ()
{
unsafe
{
int x = sizeof (decimal);
Console.WriteLine ("sizeof decimial = " + x);
}
//
int Var = 2;
//
//  Show the starting value
Console.WriteLine ("At the beginning, Var = {0}", Var);
//
//  Multiply the variable by something
Var *= 12;
Console.WriteLine ("After Var *= 12, Var = {0}", Var);
//
//  Add something to the variable
Var += 42;
Console.WriteLine ("After Var += 42, Var = {0}", Var);
//
//  Divide the variable by something
Var /= 6;
Console.WriteLine ("After Var /= 6, Var = {0}", Var);
//
//  Shift the bits in the variable four spaces to the left
//  This is the same as multiplying by 16 (2 to the fourth power)
Var <<= 4;
Console.WriteLine ("After Var <<= 4, Var = {0}", Var);
//
//  Shift the bits in the variable four spaces to the right using
//  and expression on the right. This is the same as dividing
//  by 16.
int Shift = 3;
Var >>= Shift + 1;
Console.WriteLine ("After Var >>= Shift + 1, Var = {0}", Var);
//
//  Modulo divide the variable by something
Var %= 6;
Console.WriteLine ("After Var %= 6, Var = {0}", Var);
}
}
}

Related examples in the same category

 1 The + Operator Is Left Associative 2 Math Operators with int value 3 Demonstrate the difference between prefix postfix forms of ++ 4 Demonstrate the relational and logical operators 5 Demonstrate the short-circuit operators 6 Side-effects can be important 7 Prevent a division by zero using the ? 1 8 Self increment 9 Self decrease 10 Illustrates the use of the comparison operators 11 Illustrates the use of the Boolean logical operators 12 Ternary operator 13 Illustrates the use of the ternary operator 14 Illustrates the use of the arithmetic operators 15 Illustrates the use of the bitwise operators 16 Illustrates the use of the shortcut operators 17 Prefix and postfix versions of the increment and decrement operators 18 Operator precedence 19 Relational Operators 3 20 Numeric Operators 3 21 Numeric Operators 1 22 Relational Operators 23 control the operator evaluation sequence 24 'logical and' and bitwise and 25 logical or and bitwise or 26 And Assignment 27 Or Assignment 28 Iff operator in C# 29 logical exclusive-or 30 Conditional operator and Math calculation 31 Add Assigment for both number and string value