Professional Documents
Culture Documents
Chapter 2: Variables & Arithmetic
Chapter 2: Variables & Arithmetic
namespace HelloWorldProgram
{
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("What Up Class!");
int nStop = Console.Read();
}
}
}
OK – That is out of the way
Let’s Move on
Decimal Types
– New – Up to 28 digits of precision (128 bits used to represent the number)
– Decimal place can be included
But unlike Float or Decimal, there is no conversion
The Precision and the Decimal place go together
For literals, put an “M” at the end to state that the number is a decimal
Unless you really need to use very very big number or very very small numbers, suggest you
use Decimal instead of Double of Float
– But you’ll still be responsible for understanding the different!!
Boolean
– Value of either true or false
– You must use true or false, not 0, 1 or -1
Char
– Single Unicode Character (2 Bytes)
What is Unicode??
In Assignment, Single Quotes are used
– Char bChar = ‘B’;
Reference Variable Types
In C#
– All Variable Types are actually Classes
Which implies that when you define a variable you are
really creating an Instance of an Class
Variable/Identifier Names
Numbers, Letters (upper or lower case) and the underscore (_)
Must begin with either a letter or an underscore.
Cannot be a reserved word
Should be indicative of what the variable is storing
Capitalization Counts!!!
– Variable1, variable1 and VARIABLE1 are three different
variables!!!
Try not to use run on variable names
– costofliving – use cost_of_living or costOfLiving
– currentinterestrate – use current_interest_rate or
currentInterestRate
Constants
+, -, /, * and % (Modulas/Remainder)
Exponentiation is a handled through method calls
/ with integral data will truncate the remainder
Standard order of precedence
– Parenthesis then
– Unary operators (-) then
– * / % from left to right then
– + - from left to right
Mixed Expressions
int demo;
demo = 3 * 5 + 2;
demo = 3 * (5 + 2);
Assignment Statements
if (TicketsOnSale == null)
{
AvailableTickets = 0;
}
else
{
AvailableTickets = (int)TicketsOnSale;
}
using System;
class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;
//Using null coalesce operator ??
AvailableTickets = TicketsOnSale ?? 0;
Console.WriteLine("Available Tickets={0}", AvailableTickets);
}
}