Intro Week 2
Contents
Introducing C#
Compiled vs interpreted languages.
Which is faster?
Reading a book a line at a time.
Reading a book a page at a time.
An interpreted language reads every line every time as the program runs the interpretor executes each line it reads. A compiled language is compiles the lines of code into and executable that can be run.
Visual Studio
Projects (.prj) and Solutions (.sln, .sou)
A Visual Studio Solution contains 1 or more projects.
http://msdn.microsoft.com/en-us/library/xhkhh4zs.aspx
A project contains files needed to build the project.
Fiel types for c# and vb.net solutions
Logic Diagramming
Common Programming Language Tip excercise
Drawing Excercise
pseudo code
http://commons.wikimedia.org/wiki/Logic_diagram
Demo in viseo
Types of Data
Everything is an Object
In c# everything is an object. And all objects inherit from the object class.
See object in the classbrowser
Since all objects inherit from the object class they all have some the basic functionality like a method called ToString();
See the source for object.cs from mono
Basic Data Types
C# is a strongly typed language. This means every object in C# must be declared to be of a specific type. All of c# basic varible type inherit from System.Object
Variable Types
Type | Size in Bytes | .Net Type | Description |
---|---|---|---|
byte | 1 | Byte | Unsigned (0-255) |
char | 2 | Char | Unicode Characters ascii unicode and other |
bool | 1 | Boolean | True of False (Note: C# boolean values do not equate to integer values, or True != (read as is not equal to) 1 and False != 0) |
sbyte | 1 | SByte | Signed integers(-128 to 127) |
short | 2 | Int16 | Signed integers(-32,768 to 32,767) |
ushort | 2 | UInt16 | Unsigned integers(0 to 65,535) |
int | 4 | Int32 | Signed integers(-2,147,483,648 to 2,147,483,647) |
uint | 4 | UInt32 | Unsigned integers(0 to 4,294,967,295) |
float | 4 | Single | fixed-precision up to 7 digits. Floating point number ( 1.5 x 10-45 to 3.4 x 1038 ) |
double | 8 | Double | fixed-precision up to 16 digits. Floating point number ( 5.0 x 10-324 to 1.7 x 10308 ) |
decimal | 12 | Decimal | fixed-precision up to 28 digits. Typically used for financial calculations. Required the suffix "m" or "M" |
long | 8 | Int64 | Signed integer ( -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) |
ulong | 8 | UInt64 | Unsigned integer (0 to 18,446,744,073,709,551,615 ) |
Variables must be declared with an identifier and then initialized.
Declaration
Declaration sets aside a named section of memory the is the proper size to hold the declared type. At this point the variable contains nothing.
// declare a variable
int firstInt; //declares a vaiable of type int
string myString; //declares a vaiable of type string
Initialization
Initialization actually sets the variables value
// initialize the variable
firstInt = 1;
myString = "Hello!";
Initialization uses the assignment operator to set the value of a variable
Assignment
The Assignment operator in c# is the '=' sign. You can assign variables like this...
Assignment
The Assignment operator in c# is the '=' sign. You can assign variables like this... We'll learn more about operators later.
other ways to do it
// declare some variables
int secondInt, thirdInt, fourthInt;
secondInt = 2;
thirdInt = 3;
fourthInt = 4;
//declare and initialize variables in one line
int myNegativeInt = -2147483648;
In c# variables cannot be used unil they are initialized. For example
Branching
evil goto - I won't show it figure it out on your own...
- if
- switch
Looping
- for
- while
- do... while
- foreach
Branching Statements
if
syntax
if (expression)
// statement
if (expression) {
// statements
// statements
}
if (expression) {
// statements
// statements
}
else {
// statements
}
About braces and indenting. I usually use BSD/Allman Style.
Jargon File
indent style n.
The One True Brace Style
Console Input There are several ways to get data into your program. One of the simplest is to have someone type it into the console. The frame work supports Console.ReadLine() this method reads on line from the input of the console.
EchoOnce.cs
Another popular way to get data into your program is to send it in as an argument when the program is run.
HelloName.cs
HelloNameSafe.cs
Homework
Programming Assignment – Number Add Write the logic diagram to obtain 3 numbers from the user and then display the sum of the three numbers.
Code a C# console application from your logic diagram.