Chapter 1 demonstrated how to use statements to get the computer to do something. The statements were made up of keywords and symbols that end with a semicolon. The next step is to examine expressions.
An expression uses operators and operands to instruct the compiler to perform a computation. Operators act on operands. Operators come in three flavors:
Unary: A unary operator has one operand.
Binary: A binary operator has two operands.
Ternary: A ternary operator has three operands.
The operands consist of variables, constants, and method calls. The following shows examples of expressions.
using System;
namespace Client.Chapter_2___ Expressions_and_Operators
{
class Expressions
{
static void Main(string[] args)
{
int MyInt = 12345;
int MyInt2 = 10000;
int Sum = 0;
long MyLong = MyInt;
short MyShort = (short)MyInt;
//Bool expression
if (MyInt == MyInt2)
{
//Simple calculation
Sum = MyInt + MyInt2;
}
else
{
Sum = MyInt - MyInt2;
}
}
}
}