the c# language fundamentals ( i ) p.f. tsai. hello world project please follow the instructions...
TRANSCRIPT
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 project
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
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)
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
Intrinsic types (primitives)
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;
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
Escape characters
Exercise 1 Please try to print the following mess
age “Hello World!” (and BEEP!) trick: Console.Read();
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
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
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.
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”);
More about WriteLine()
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 …
Variables and Constants
Initializing and assigning a value to a variable identifier case 1, 2 & 3
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
Case 1
Case 2
Case 3
Variables and Constants Constants
A constant is a variable whose value can not be changed
Memorizing names is much easier than numbers
Example
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 );
Enumerations
An alternative to constants Value type A set of named constants
(enumerator list) Restricted to integer types
except for char
Examples
Declare an enumeration
define the type of constants
Example codes
Access the constant by a dot operator
Example 3-5 in the textbook (buggy)
Declared outside Main()
(int)(int)
Special case
01
21
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
If - else
Unlike Matlab, it doesn’t need end
Can be nested The code block
use { } to mark the statement
Example codes
Operators
will discussed later
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.
References
C# programming 清華大學金仲達教授課程教材 嵌入式軟體聯盟 (Embedded Software
Consortium)