Demonstrates compound assignment operators

image_pdfimage_print

   
 
/*
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);
  }
//
//  Start with an integer variable
            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);
        }
    }
}