Documentos de Académico
Documentos de Profesional
Documentos de Cultura
A Faster Start
C# does NOT require the use of pointers,
object destructors, and the use of #include
Command prompt or Visual Studio IDE
No String[] args needed in main method
header as in Java (optional)
Class name does not have to match file name
Can use .exe name to execute in the
command line interface
File can contain multiple classes
3
Console is a class
Console defines the attributes of a collection of
similar Console objects
8
Selecting Identifiers
11
Variables
All variables are objects
As objects, all types have built-in methods
Creating formatted output is easy
Variables.cs
Characters are not one byte, they are two
bytes due to Unicode (0 to 65535)
Global Portability
12
Data Types
byte
1 byte
Range
0 to 255
Unsigned
byte
sbyte
1 byte
Range
-128 to 127
Signed
byte
short
2 bytes
Range
Signed
-32768 to 32767 short
ushort 2 bytes
Range
0 to 65535
Unsigned
short
13
4 bytes
Range
-2,147,483,647 to
2,147,483,647
uint
4 bytes
Range
0 to 4,294,967,295
Signed
integer
Unsigned
integer
long
8 bytes
Greater than
900,000 trillion
Signed
long int
ulong
8 bytes
Greater than 18
million trillion
Unsigned
long int
14
float
4 bytes Range
double
8
bytes
Range
decimal 8
bytes
Range
string
char
Range N/A
Range
bool
N/A
2
bytes
Float
number
Double
precision
Fixed
precision
Unicode
0x0000 to 0xFFFF
Unicode
character
True or False
Boolean
15
Decimal.cs
C++ often was subject to a variety of rounding
errors. The decimal data type can accurately
represent up to 28 decimal points.
17
Formatting Output
Format
Character
Description
C or c
Currency
($##,###.##)
D or d
Decimal
[-]########
E or e
Scientific
(exponential)
[-]#.#######E+###
[-]#.######e+###
F or f
Fixed Point
[-]########.##
18
Example of Formatting
Console.WriteLine(Floating Amount:
{0,0:$###.##}, 3.14);
Prints: Floating Amount: $3.14
Console.WriteLine(Floating Zeros:
{0,0:$000.00}, 3.14);
Prints: Floating Amount: $003.14
19
Description
G or g
General
N or n
Number
[-]##,###.##
P or p
Percent
R or r
Round trip
X or x
Hexadecimal
20
Examples of Formatting
double someMoney = 123;
string moneyString;
moneyString = someMoney.ToString("F");
Console.WriteLine(moneyString);
moneyString = someMoney.ToString("F3");
Console.WriteLine(moneyString);
Console.WriteLine(someMoney.toString("C2")
The first WriteLine() statement in the following
code produces 123.00, the second produces
123.000, and the third produce $123.00.
21
Boolean Variables
Values are true/false (no 1/0 like C++)
If statements must use == for
equivalency
If you fail to use ==, it will not compile
Attempting to use an uninitialized variable
If.cs
23
Relational Operators
24
Escape Sequences
25
If Statement
Like C++ and Java
IfElseDecision.cs
IfElseDecisionWithInput.cs
Comments can be written 3 ways
/* The old C way Block Comments */
// The C++ way Line Comments
/// The XML way
27
Operator
Logical
Operators
Meaning Effect
&&
AND
||
OR
NOT
29
Switch Statement
No fall-through rule
Switch.cs
30
Loops
While Loop
Do While Loop
For Loop
A loop is a structure that allows
repeated execution of a block of
statements
31
32
33
35
36
Methods
All methods must have an explicit return type
No int default when returning variables
Methods can have four types of arguments:
Value
ParameterDemo1.cs
Reference
ParameterDemo2.cs
Output
ParameterDemo3.cs
Params
ParameterDemo4.cs
37
Overloading Methods
Overloading involves using one term to
indicate diverse meanings
When you overload a C# method, you
write multiple methods with a shared name
The compiler understands the meaning
based on the arguments you use with the
method
38
Overloading Methods
An overloaded method
39
Initializing an Array
Arrays, like object fields, have default values
You can assign nondefault values to array
elements upon creation
Examples:
int[] myScores = new int[5] {100,76,88,100,90};
int[] myScores = new int[] {100,76,88,100,90};
int[] myScores = {100,76,88,100,90};
40
Arrays
Arrays must be declared with brackets
after the type:
int[] numbers = new int[20];
Can use foreach statement (like VB)
ForEach.cs
Params
Params.cs
41
Arrays
"Extra" array elements are not initialized to 0
IndexOutOfBoundsException becomes
IndexOutOfRangeException
Garbage Collection
No need for a delete
command to clear up dynamic
arrays
C#s garbage collection
reclaims object space
automatically behind the
scenes
For efficiency, C# only runs
the garbage collection feature
when:
There are objects to recycle
There is a need to recycle them
43
44
45
BinarySearch program
46
47
48
Output of MyTestScores
49
Classes
Static members cannot be used with
objects
Built-in components have "natural"
names - e.g., Label, not JLabel
No multiple constructors for
components like Label, Button, etc.
50
51
54
Understanding Constructor
Methods
A constructor method is a method that establishes
an object
Every class created is automatically supplied with a
public constructor method with no parameters
Any constructor method you write must have the
same name as its class, and constructor methods
cannot have a return type
55
Inheritance
Use a colon (not "extends" or
"implements")
Classes can appear in any order and
within the same file (no forward
references)
When child class method overrides
parent class method you must use the
keyword new or override
56
Extending Classes
When you create a class that is an extension or
child of another class, you use a single colon
between the derived class name and its base
class name
57