/*
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);
}
}
Month: February 2011
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]
Ternary operator
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class ThreeInputValues
{
static void Main()
{
int valueOne = 10;
int valueTwo = 20;
int maxValue = valueOne > valueTwo ? valueOne : valueTwo;
Console.WriteLine("ValueOne: {0}, valueTwo: {1}, maxValue: {2}",
valueOne, valueTwo, maxValue);
}
}

