BACS 387 - University of Northern Colorado

Download Report

Transcript BACS 387 - University of Northern Colorado

Language Elements 1
Keywords, variables, & data types









Learn the basic structure of a C# program.
Learn how the various C# elements are related to
each other.
Learn the basic syntax of C# programs.
Learn about Statements and Blocks.
Learn about Comments.
Learn about Variables and Constants.
Learn about predefined and user defined data types
Learn about stack and heap, value and reference
storage
Learn the basics of type casting.
7/18/2015
OO Programming
2
using system;
Identify Framework namespace used
namespace SimpleProgram
Define project namespace
{
Define class to be created
class MyClass
{
Define method to be created
static void Main()
(Main in this case)
{
Console.WriteLine(“Program Statements”);
}
}
}
Statements that make up
the method
Brackets to indicate blocks
7/18/2015
OO Programming
3
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
}
}
}
Last update: 7/18/2015
OO Programming
4
Last update: 7/18/2015
OO Programming
5
Creating C# programs requires you to follow the
language syntax.
 This syntax is made up of some basic language
elements:








Identifiers
Keywords
Whitespace
Comments
Statements & Blocks
Variables
Constants
Last update: 7/18/2015
OO Programming
6
Identifiers are character strings used to name things
in your program.
 They are case sensitive, so

 TotalAddCnt
 TotalAddcnt
 TotaladdCnt
are totally different.
 The first character can be a-z, A-Z, _, or @.
 Subsequent characters can be a-z, A-Z, 0-9, or _.
 Various naming styles are popular. These will be
covered later.
7/18/2015
OO Programming
7
VALID IDENTIFIERS
INVALID IDENTIFIERS
HighTemp
stuName
 _LowTemp
 Last_Name
 @My_Address
 Home4Me
 @_____




7/18/2015
9_News
stu-Name
 -LowTemp
 Last/Name
 total@home
 4Sale
 @__@__
OO Programming
8
Keywords are the character string tokens used to
define the C# language.
 Keywords cannot be used as variable names or any
other type of identifier.
 All C# keywords are only lower case letters.
 .NET type names use Pascal casing (e.g., MyValue,
TotalStudentCount).

7/18/2015
OO Programming
9











7/18/2015
byte
class
const
do
else
false
for
foreach
if
int
namespace












OO Programming
null
object
public
ref
return
static
string
switch
true
void
while
...and many more…
10







A statement is a source code instruction that tells the
computer to perform an action.
Simple statements end in a semi-colon ( ; ).
If your program needs to perform more than one action,
you can combine several simple statements into a code
Block.
Blocks consist of zero or more statements enclosed by a
pair of curly braces { }.
You can use a block anywhere the syntax requires a
statement.
Some programming constructs require blocks and do
not allow simple statements.
You do not put a ; after the { } of a block.
7/18/2015
OO Programming
11





int MyVar1 = 7;
system.console.writeline(“This is a statement”);
// comments are not statements (so no semi-colon)
string LastName;
class AverageTemp
{
private int High = 90;
private int Low = 50;
return (High + Low) / 2;
}
7/18/2015
OO Programming
12
“Whitespace” in a program refers to characters that
do not have visible output.
 C# ignores whitespace in your source code;
however, you should still use it to make your
programs more readable.
 The common whitespace characters include:





7/18/2015
Space(s)
Tab
New line character
Carriage return
OO Programming
13




Comments improve the readability of the program.
They also help others understand the logic.
In C#, comments are ignored by the compiler.
There are 3 types of comments:
// single line comments appear anywhere in the line
/*
Delimited comments
can span multiple lines, but end
with
*/
/// XML comments are used to produce
/// program documentation
7/18/2015
OO Programming
14
Variables are named memory locations that hold
changeable values.
 Variables must be declared before they are used.
 Local variables (i.e., ones defined in a method) must
be explicitly initialized before they can be used.
The syntax for defining and assigning variables is:

[scope] data-type variable-name
Examples:
int age = 22;
public bool isGood = true;
string myName = “Jay”;
decimal expenses;
7/18/2015
OO Programming
[=
value];
15




Scope identifiers are also called “Access Modifiers”
There are 5 access modifier categories





private
public
protected
internal
protected internal
This is an optional component
If you do not specify the scope identifier, it is “private”
by default
Examples (identical result):
int age = 22;
private int age = 22;
7/18/2015
OO Programming
16





Private means that it can only be accessed within the
class where it is defined.
Protected is like private except that members derived
from its class may also see the value (derived classes
have access).
Internal means that it can only be accessed within its
own assembly
Protected Internal means it is visible from all derived
classes (like protected) and also all classes within the
assembly (like internal).
Public means that it can be accessed from any assembly
in the system.
Last update: 7/18/2015
OO Programming
17
Value Types
Predefined Types
User-Defined Types
7/18/2015
byte
sbyte
short
ushort
int
uint
long
ulong
float
double
decimal
char
bool
struct
enum
Reference Types
object
string
class
interface
delegate
array
OO Programming
18
7/18/2015
OO Programming
19

Integer Data Types
Data Type
Suffix
Value Range
Size in Memory
byte
0 to 255
8-bit (1 byte)
sbyte
-128 to 127
8-bit (1 byte)
short
-32,768 to 32,767
16-bit (2 bytes)
ushort
0 to 65,535
16-bit (2 bytes)
-2,147,483,648 to 2,147,483,647
32-bit (4 bytes)
int
7/18/2015
uint
U
0 to 4,294,967,295
32-bit (4 bytes)
long
L
64-bit (8 bytes)
ulong
U
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
0 to 18,446,744,073,709,551,615
OO Programming
64-bit (8 bytes)
20

Floating Data Types
Data Type

Value Range
Size in Memory
float
±1.5 X 10 -45 to ±3.4 X 10 38
32-bit (4 bytes)
double
±5 X 10 -324 to ±1.7 X 10 308
64-bit (8 bytes)
Other Types
Data Type
bool
char
decimal
Data Type
object
string
7/18/2015
Value Range
Size in Memory
true, false
8-bit (1 bytes)
U+0000 to U+ffff (i.e., 1
16-bit (2 bytes)
character of full character set)
96-bit (12 bytes)
±1.0 X 10 -28 to ± 7.9 X 10 28
Meaning
Can store any type in an
object variable
A set of characters
OO Programming
Size in Memory
varies
1 byte per character
21

There are 6 user-defined types that you can create in C#.
 class types
 struct types
 array types
 enum types
 delegate types
 interface types

These are created using a type declaration that includes:
 The kind of type being created
 The name of the new type
 A declaration (name and specification) of the type’s members
7/18/2015
OO Programming
22


Running programs must store data in memory.
In C#, running programs store data in two regions:
 Stack
 Heap
The stack is an array of memory that uses last-in,
fist-out (LIFO) structure.
 The heap is an area when chunks of memory are
allocated to store certain types of data.
 Heap data can be accessed in any order.

7/18/2015
OO Programming
23
7/18/2015
OO Programming
24
7/18/2015
OO Programming
25
7/18/2015
OO Programming
26
7/18/2015
OO Programming
27
7/18/2015
OO Programming
28
C# is a strongly typed language. This means that it is
extremely strict on defining and converting data
types.
 Implicit cast

 Conversion (called “casting”) is implicitly allowed as long as
there is no loss of data.
 Automatic transformation that occurs when a value is assigned
to a type with higher precedence.

Explicit cast
 Handled by special syntax supplied by the programmer.
 This syntax requires you to place the desired result type in
parentheses followed by the variable or constant to be cast.
Example: int i = (int)longValue;
Last update: 7/18/2015
OO Programming
31

Supported implicit conversions:
From
To
sbyte
short, int, long, float, double, decimal
byte
short, ushort, int, uint, long, ulong, float, double, decimal
short
int, long, float, double, decimal
ushort
int, uint, long, ulong, float, double, decimal
int
long, float, double, decimal
uint
long, ulong, float, double, decimal
long, ulong
float, double, decimal
float
double
char
ushort, int, uint, long, ulong, float, double, decimal
Last update: 7/18/2015
OO Programming
32

Arithmetic with variables or constants of the same
type:
 Will produce a result that retains the same type.

Arithmetic with operands of dissimilar types:
 Require C# to choose a unifying type for the result.
 C# implicitly (or automatically) converts nonconforming
operands to the unifying type (i.e., the type with the higher type
precedence).

“Casting” type precedence from less precise to more
precise data types follows these rules:
 byteshortintlongfloatdoubledecimal
 charint
Last update: 7/18/2015
OO Programming
33

Constants are like variables, except that once
they have been assigned, the value of a constant
cannot be changed during the execution of the
program.
The syntax for defining and assigning constants is:
[scope] CONST data-type constant-name = value;
Examples:
const decimal interest = 5.3;
public const char = ‘G’;
const string company = “Ace Reality Corporation”;
7/18/2015
OO Programming
34
A C# program is a structured collection of identifiers and
keywords organized into valid statements.
 C# statements end in a semi-colon ( ; ). C# blocks
can be placed anywhere a statement is valid. Blocks
are code surrounded by { }.
 C# variables can be defined using a variety of
predefined and user-defined types.
 Conversion from one type to another follows specific
“casting rules.”
 Constants can also be defined using these types.
 Standard scoping rules are used in all C# type
declarations.

7/18/2015
OO Programming
35