the c# language fundamentals ( i ) p.f. tsai. hello world project please follow the instructions...

34
The C# language fundamentals ( I ) P.F. Tsai

Upload: may-newman

Post on 03-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

The C# language fundamentals

( I )

P.F. Tsai

Page 2: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Hello World Project

Please follow the instructions mentioned in the previous class to build a hello world project

Page 3: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Types C# is a strong typed language

Weak Type: a value of one type is a bit-pattern which can be re-interpreted as a value of another type (C)

Strong Type: a cast always involves a compile-time or run-time compatibility check, and a meaningful conversion. (JAVA & C#)

Two sets of Types Intrinsic (built-in) types User-defined types Value types vs Reference types

Stack vs Managed heap Declaration

Page 4: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Stack and Heap Stack

The stack data structure First In Last Out

Memory An area of memory supported by CPU and used to

store local variables Pointed by the SP register Referred by the variable names

Heap An area of memory managed by VM

Garbage collector (GC)

Page 5: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Value types vs Reference types All intrinsic types are value types except for

Object and String All user-defined types are reference types e

xcept for structs Examples:

int myAge = 33; Variable contains the value

string myName = “Mandy”; Variable contains the address of the value

Page 6: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Intrinsic types (primitives)

Page 7: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Types frequently used bool

True and false short, int & long

Depends on your need Ram is CHEAP but time is EXPENSIVE

Double, float(f) & decimal(m) Double is default float somefloat = 57f;

Page 8: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Types frequently used The char type represents a Unicode

Unicode provides a unique number for every character

no matter what the platform, program & language are http://www.unicode.org/standard/translations/t-chines

e.html Example:

char myChar = ‘A’; // ‘\u0041’ use single quote ‘ ‘

Escape characters For special purposes

Page 9: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Escape characters

Page 10: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Exercise 1 Please try to print the following mess

age “Hello World!” (and BEEP!) trick: Console.Read();

Page 11: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Exercise 2

Please try to store your personal data into variables with corresponding types your name, age, gender, marriage,

phone number, height, weight, birthday & your home address

Ex: string name = “Mandy Tsai”; // use

double quote

Page 12: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Converting intrinsic types

Objects of one type can possibly be converted into another type Implicit cast Explicit cast

Example short x = 5; int y = x; //implicit int x = 5; short y = (short) x; // explicit

Page 13: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Exercise 3

Please try to figure out the value range of a short type variable and modify the above example (the explicit one) with assigning x a value outside the short-type range to see what happens then.

Page 14: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

WriteLine() A formatted output

Console.WriteLine(“I am {0} years old and she is {1} years old“, myAge, herAge);

An easier way Console.WriteLine(“I am “+myAge+” y

ears old and she is “+herAge+” years old”);

Page 15: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

More about WriteLine()

Page 16: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Exercise 4 Now you can modify your exercise

2 to show your personal information on the screen

Example Name : Mandy Tsai Age : 33 Gender: Male …

Page 17: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Variables and Constants

Initializing and assigning a value to a variable identifier case 1, 2 & 3

Page 18: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Identifer Names of your types, variables, methods, c

onstants, objects and so forth Naming convection

Camel notation for variables myVariable

Pascal notation for methods and others MyFunctionName

No clash with keywords

Page 19: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Case 1

Page 20: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Case 2

Page 21: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Case 3

Page 22: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Variables and Constants Constants

A constant is a variable whose value can not be changed

Memorizing names is much easier than numbers

Page 23: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Example

Page 24: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Exercise 5

Please try to implement the above example and modify the constant value to see what happens. FreezingPoint = FreezingPoint + 1; System.Console.WriteLine("Freezing poi

nt of water: {0}", FreezingPoint );

Page 25: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Enumerations

An alternative to constants Value type A set of named constants

(enumerator list) Restricted to integer types

except for char

Page 26: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Examples

Declare an enumeration

define the type of constants

Page 27: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Example codes

Access the constant by a dot operator

Example 3-5 in the textbook (buggy)

Declared outside Main()

(int)(int)

Page 28: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Special case

01

21

Page 29: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Conditional branching statements

A conditional branch is created by a conditional statement, which is signaled by keywords such as if, else, or switch

A conditional branch occurs only if the condition expression evaluates true Only boolean values are accepted here

Page 30: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

If - else

Unlike Matlab, it doesn’t need end

Can be nested The code block

use { } to mark the statement

Page 31: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Example codes

Page 32: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Operators

will discussed later

Page 33: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

Exercise 6 You need to write a program to evaluate the

temperature of a batch tank, and specifically to return the following types of information: If the temperature is 50 degrees or lower, the pr

ogram should warn you about temperature is way too low.

If the temperature is between 50 to 70 degrees, the program should tell you that the process is under control.

If the temperature is 70 degrees or higher, the program should warn you about temperature is way too high.

Page 34: The C# language fundamentals ( I ) P.F. Tsai. Hello World Project Please follow the instructions mentioned in the previous class to build a hello world

References

C# programming 清華大學金仲達教授課程教材 嵌入式軟體聯盟 (Embedded Software

Consortium)