Windows Programming Chapter Two
Windows Programming Chapter Two
7
Using the Standard Binary
Arithmetic Operators
8
Using Shortcut Arithmetic
Operators
9
Using the Boolean Data Type
10
Using Floating-Point Data Types
11
Formatting Floating-Point Values
12
Formatting Floating-Point Values
13
Understanding Numeric Type
Conversion
14
Understanding Numeric Type
Conversion
15
Using the char Data Type
16
Using the char Data Type
17
Using the string Data Type
18
Using the string Data Type
20
Using the string Data Type
23
Defining Named Constants
SalesTax program
24
Defining Named Constants
25
Accepting Console Input
26
Accepting Console Input
27
InteractiveSalesTax program
Accepting Console Input
28
Accepting Console Input
29
C# Basic Structure
usings (with semi-colon)
namespace FirstProgram
{
class FirstProgram
{
static void Main(string[] args)
{
//Do some stuff
}
}
}
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);
}
}