Download presentation
Presentation is loading. Please wait.
Published byMay Newman Modified over 9 years ago
1
The C# language fundamentals ( I ) P.F. Tsai
2
Hello World Project Please follow the instructions mentioned in the previous class to build a hello world project
3
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
4
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)
5
Value types vs Reference types All intrinsic types are value types except for Object and String All user-defined types are reference types except for structs Examples: int myAge = 33; Variable contains the value string myName = “ Mandy ” ; Variable contains the address of the value
6
Intrinsic types (primitives)
7
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;
8
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- chinese.html Example: char myChar = ‘ A ’ ; // ‘ \u0041 ’ use single quote ‘ ‘ Escape characters For special purposes
9
Escape characters
10
Exercise 1 Please try to print the following message “ Hello World! ” (and BEEP!) trick: Console.Read();
11
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
12
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
13
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.
14
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+ ” years old and she is “ +herAge+ ” years old ” );
15
More about WriteLine()
16
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 …
17
Variables and Constants Initializing and assigning a value to a variable identifier case 1, 2 & 3
18
Identifer Names of your types, variables, methods, constants, objects and so forth Naming convection Camel notation for variables myVariable Pascal notation for methods and others MyFunctionName No clash with keywords
19
Case 1
20
Case 2
21
Case 3
22
Variables and Constants Constants A constant is a variable whose value can not be changed Memorizing names is much easier than numbers
23
Example
24
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 point of water: {0}", FreezingPoint );
25
Enumerations An alternative to constants Value type A set of named constants (enumerator list) Restricted to integer types except for char
26
Examples Declare an enumeration define the type of constants
27
Example codes Access the constant by a dot operator Example 3-5 in the textbook (buggy) Declared outside Main() (int)
28
Special case 0 1 21
29
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
30
If - else Unlike Matlab, it doesn ’ t need end Can be nested The code block use { } to mark the statement
31
Example codes
32
Operators will discussed later
33
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 program 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.
34
References C# programming 清華大學金仲達教授課程教材 嵌入式軟體聯盟 (Embedded Software Consortium)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.