/*
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);
}
}
}
Language Basics
Operator precedence
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_10.cs illustrates operator precedence
*/
public class Example3_10
{
public static void Main()
{
int myInt = 2 + 5 * 10;
System.Console.WriteLine("2 + 5 * 10 = " + myInt);
myInt = (2 + 5) * 10;
System.Console.WriteLine("(2 + 5) * 10 = " + myInt);
myInt = 2 * 20 / 5;
System.Console.WriteLine("2 * 20 / 5 = " + myInt);
}
}
Prefix and postfix versions of the increment and decrement operators
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_8.cs illustrates the use of
prefix and postfix versions of the
increment and decrement operators
*/
public class Example3_8
{
public static void Main()
{
// postfix increment
int length = 3;
int newLength = length++;
System.Console.WriteLine("Postfix increment example");
System.Console.WriteLine("length = " + length);
System.Console.WriteLine("newLength = " + newLength);
// prefix increment
length = 3;
newLength = ++length;
System.Console.WriteLine("Prefix increment example");
System.Console.WriteLine("length = " + length);
System.Console.WriteLine("newLength = " + newLength);
// postfix decrement
length = 3;
newLength = length--;
System.Console.WriteLine("Postfix decrement example");
System.Console.WriteLine("length = " + length);
System.Console.WriteLine("newLength = " + newLength);
// prefix decrement
length = 3;
newLength = --length;
System.Console.WriteLine("Prefix decrement example");
System.Console.WriteLine("length = " + length);
System.Console.WriteLine("newLength = " + newLength);
}
}
Illustrates the use of the shortcut operators
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_7.cs illustrates the use of
the shortcut operators
*/
public class Example3_7
{
public static void Main()
{
int length = 1;
length += 10;
System.Console.WriteLine("length = " + length);
length *= 2; // multiplies length by 2
System.Console.WriteLine("length = " + length);
length /= 3; // divides length by 3
System.Console.WriteLine("length = " + length);
}
}
Illustrates the use of the bitwise operators
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_6.cs illustrates the use of
the bitwise operators
*/
public class Example3_6
{
public static void Main()
{
byte byte1 = 0x9a; // binary 10011010, decimal 154
byte byte2 = 0xdb; // binary 11011011, decimal 219
byte result;
System.Console.WriteLine("byte1 = " + byte1);
System.Console.WriteLine("byte2 = " + byte2);
// bitwise AND
result = (byte) (byte1 & byte2);
System.Console.WriteLine("byte1 & byte2 = " + result);
// bitwise OR
result = (byte) (byte1 | byte2);
System.Console.WriteLine("byte1 | byte2 = " + result);
// bitwise exclusive OR
result = (byte) (byte1 ^ byte2);
System.Console.WriteLine("byte1 ^ byte2 = " + result);
// bitwise NOT
result = (byte) ~byte1;
System.Console.WriteLine("~byte1 = " + result);
// left shift
result = (byte) (byte1 << 1);
System.Console.WriteLine("byte1 << 1 = " + result);
// right shift
result = (byte) (byte1 >> 1);
System.Console.WriteLine("byte1 >> 1 = " + result);
}
}
Illustrates the use of the arithmetic operators
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_2.csc illustrates the use of
the arithmetic operators
*/
public class Example3_2
{
public static void Main()
{
// integers and arithmetic operators
System.Console.WriteLine("10 / 3 = " + 10 / 3);
System.Console.WriteLine("10 % 3 = " + 10 % 3);
int intValue1 = 10;
int intValue2 = 3;
System.Console.WriteLine("intValue1 / intValue2 = " +
intValue1 / intValue2);
System.Console.WriteLine("intValue1 % intValue2 = " +
intValue1 % intValue2);
// floats and arithmetic operators
System.Console.WriteLine("10f / 3f = " + 10f / 3f);
float floatValue1 = 10f;
float floatValue2 = 3f;
System.Console.WriteLine("floatValue1 / floatValue2 = " +
floatValue1 / floatValue2);
// doubles and arithmetic operators
System.Console.WriteLine("10d / 3d = " + 10d / 3d);
System.Console.WriteLine("10.0 / 3.0 = " + 10.0 / 3.0);
double doubleValue1 = 10;
double doubleValue2 = 3;
System.Console.WriteLine("doubleValue1 / doubleValue2 = " +
doubleValue1 / doubleValue2);
// decimals and arithmetic operators
System.Console.WriteLine("10m / 3m = " + 10m / 3m);
decimal decimalValue1 = 10;
decimal decimalValue2 = 3;
System.Console.WriteLine("decimalValue1 / decimalValue2 = " +
decimalValue1 / decimalValue2);
// multiple arithmetic operators
System.Console.WriteLine("3 * 4 / 2 = " + 3 * 4 / 2);
}
}
Illustrates the use of the ternary operator
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_5.cs illustrates the use of
the ternary operator
*/
public class Example3_5
{
public static void Main()
{
int result;
result = 10 > 1 ? 20 : 10;
System.Console.WriteLine(“result = ” + result);
result = 10 < 1 ? 20 : 10; System.Console.WriteLine("result = " + result); } } [/csharp]