the c# language fundamentals ( i ) p.f. tsai. hello world project please follow the instructions...
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/1.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/21.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/22.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/23.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/24.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/25.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/26.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/27.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/28.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/29.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/30.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/31.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/32.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/33.jpg)
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](https://reader030.vdocuments.us/reader030/viewer/2022032414/56649ef65503460f94c09ca2/html5/thumbnails/34.jpg)
References
C# programming 清華大學金仲達教授課程教材 嵌入式軟體聯盟 (Embedded Software
Consortium)