Lab 2

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

C# Types

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.

The dot notation


Integral types
• There are a number of integral types
– byte 0-255
– sbyte -128 to 127
– char 0- 65535 (used for individual
characters)
– short-32768 to +32767
– ushort 0 - 65635
– int -2,147,438,648 to + 2,147,438,647
– uint 0 – 429,967,295
– long ( a value in excess of 18 digits)
– ulong ( a really big positive integer)
Floating Point Types
• double –1797699313486231570 * 10^308
– To –4.94065645841246544 * 10 ^ -324 for negatives
– +4.94065645841246544 * 10 ^ -324 to
– +1797699313486231570 * 10^308 for positives

• float -3.4028235 * 10 ^ 38 to
– - 1.401298 * 10 ^ -45 for negative numbers

+1.401298 * 10 ^ -45 to +3.4028235 * 10 ^ 38 for positive


numbers
Strings

• string a full fledged type


– A string is a variable length and can hold up to
2 billion characters (immutable)
– Not like C or C++

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

3000 * 1000000  won’t fit in an integer


Literals and types
• Adding a suffix to a literal can force it to
the type you want.
• 45L is a long
• 45.6 is by default a double
• 45.6f Is a float
• (you will find more of these in the help)
• “string literals must be enclosed in quotes”

string s = “here is a string”;

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;

• constants must be initialized when declared.


Enumerations
enum FormState
{
NoData = 0,
Update,
Browse,
Add
}
FormState fs;

fs = FormState.Browse;

if (fs = = FormState.Browse)
{
// do something
}
White Space
• spaces
• tabs
• newlines

• white space is ignored by the compiler


Statements
• A complete instruction in C#
• ends with a semicolon ;

• int x = 8;
Identifiers
Variable names cannot be reserved words.

Thus int int;


cannot be a variable.

Use camel notation for variable names.


… int theAccountNumber;
Capitalize the first letter of class names and
methods.
Using some string methods
string a = “abcde”;
char[] cs = a.ToCharArray();
char b = cs[0]; // get first character
char c = cs[cs.GetUpperBound(0)]; // last char
a = a.ToUpper();
a = a.ToLower();
String.Format Method
• Use the string format method to build an
output string that will hold the results from
more than one variable.

Str = string.Format(“{0} is {1}”, a, b);


// if a contains “Bob” and b contains 12 then
Str = “Bob is 12”
IF
if ( expression)
statement or block;
else
statement or block;
blocks
Blocks begin and end with curly braces and can contain
multiple statements.

if ( a > 0 )
{
Console.WriteLine(“a is greater than 0”);
Console.WriteLine(“Hurrah”);
}

You might also like