Lab 2
Lab 2
Lab 2
What is a type?
The type of an object indicates
the class from which an object
was created. It indicates its
size, attributes and operations
(methods).
Types Below the Language Level
• In .NET Typing and type info exists at the
intermediate code level.
• You can create types in one language and
use them in another.
Choosing a data type
Obviously, you will choose a numeric type if you are
working with numbers. Choose a type that matches the
range of numbers you are working with.
If you are storing lots of numbers, choosing an appropriate
type can save lots of space.
Mathematical operations work faster on some numeric types
than others.
Some types have more significant digits than others.
C# vs. C++
Types are split into two categories.
Value Types
User Types
.NET objects
All types in .net give you a common syntax
format for use.
• float -3.4028235 * 10 ^ 38 to
– - 1.401298 * 10 ^ -45 for negative numbers
string x = “abcd”;
Literals and types
• By default a whole number like 45 will be
treated as an int. An integer times an integer
will yield an integer. Operations can only
be done on like numeric types (so some
calculations require converted values).
• This can create problems.
3000 * 1000000 won’t fit in an integer
Overflow
Checked and Unchecked
string s = s + “efg”;
char type
• Unicode (each character is 2 bytes)
char letter = '\u0041'; //Hex
• Escape sequences
letter = '\t'; // tab
letter = '\\'; // a backslash character
– '\n' newline
– '\”' a quote see table in the text.
Declaring variables
• int age = 3;
• long minutes = 4000000000;
• short k = 30000;
• byte s = 255;
• double distanceToSun =
93000000*5280*12;
• float rootBeer = 2.55f;
Assignment
• int x;
• int k = 5;
• k = k*x; // fails
• C# requires definite assignment.
• all variables must be assigned before use
Constants
• const int MAXAGE = 17;
fs = FormState.Browse;
if (fs = = FormState.Browse)
{
// do something
}
White Space
• spaces
• tabs
• newlines
• int x = 8;
Identifiers
Variable names cannot be reserved words.
if ( a > 0 )
{
Console.WriteLine(“a is greater than 0”);
Console.WriteLine(“Hurrah”);
}