YOMEDIA
Lecture Windows programming: Chapter 2 - Châu Thị Bảo Hà
Chia sẻ: Kiếp Này Bình Yên
| Ngày:
| Loại File: PPTX
| Số trang:68
44
lượt xem
2
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Chapter 2 introduction to the variables - constants – expressions - flow control - methods. In this chapter you will learn: Basic C# syntax variables, constants, expressions; how to branch code, loop code; how to write and call method; how to catch an exception.
AMBIENT/
Chủ đề:
Nội dung Text: Lecture Windows programming: Chapter 2 - Châu Thị Bảo Hà
- Chapter 2
Variables - Constants –
Expressions
Flow control - Methods
- What you will learn in this
chapter
•
Basic C# syntax: variables, constants,
expressions
•
How to branch code, loop code
•
How to write and call method
•
How to catch an exception
2
- Contents
1. Variables - Constants - Expressions
2. More about variables
3. Flow control
4. Methods
5. Using some classes
6. Exception
Ebook: from chapter 1 to 6 (Part I)
3
- Variables, Constants
DataType name;
•
Declare variable:
DataType name = init_value;
– example: int x, y;
– note: Variables need to be initialized before it’s
used
const DataType CONST_NAME =
value;
•
Declare constant:
– example: const double PI = 3.14;
•
Data types that are built into C# 4
- Primitive Data Types
5
- The basic variable naming rules
•
The first character of a variable name must
be either a letter, underscore characters (_)
or the at symbol (@)
•
Subsequent characters may be letters,
underscore characters, or numbers
•
Not use keywords
Right variable names: Wrong variable names:
•
Example:
myBigVar 99BottlesOfBeer
VAR1 namespace
_test It’s-All-Over
6
- Naming conventions
•
The first one, two or three letters of the
element's name indicate the type of
element; the rest of the name indicates its
purpose, and starts with a capital letter
– example: iAge, fRate, txtName, btnOK
•
Names contain multiple words
– camelCase
•
example: phoneNumber, dateOfBirth
– PascalCase
•
example: PhoneNumber, DateOfBirth
7
- Example: Using variables
class Program
{
static void Main(string[] args)
{
int myInteger;
string myString;
myInteger = 17;
myString = "\"myInteger\" is";
Console.WriteLine("{0} {1}.", myString, myInteger);
Console.ReadKey();
}
}
8
- Expression
•
Expressions are built from operators and
operands (variables or literal values)
– example: x = 2 * (a + b)
•
Operators:
– mathematical operators: +, -, *, /, %
– increment and decrement operators: ++, --
– assignment operators: =, +=, -=, *=, /=, %=
– comparison operators: !, &&, ||
– conditional operators (p.70): Exp1 ? Exp2 :
9
- Operator precedence
•
Example:
– var1 = var2 + var3 * var4;
– int var1, var2 = 5, var3 = 6;
10
- Contents
1. Variables - Constants - Expressions
2. More about variables
3. Flow control
4. Methods
5. Using some classes
6. Exception
11
- More about variables
•
Type conversion
– implicit conversion
– explicit conversion
•
Complex variable types
– enumerations
– structs
– arrays
•
String manipulation
12
- Type conversion – Implicit
conversion
•
Implicit conversion (table p.95):
var1 = var2;
– syntax:
– e.g.: Output:
ushort i; value of c: a
char c = ‘a’; value of i: 97
i = c;
Console.WriteLine("value of c: {0}", c);
Console.WriteLine("value of i: {0}", i);
– bool and string have no implicit conversions
– if B=A: any type A whose range of possible
values completely fits inside the range
13 of
- Type conversion – Explicit
conversion [1]
•
Example:
byte byteVar; // 8 bytes
short shortVar = 7; // 16 bytes
byteVar = shortVar;
The C# compiler can detect missing explicit
conversions
14
- Type conversion – Explicit
conversion [2]
•
Explicit conversion (p.96)
(DataType) variable
– Syntax:
•
Example 1:
byte byteVar;
short shortVar = 7;
byteVar = (byte) shortVar;
•
Example 2:
float x = (float)7/2;
15
- Type conversion – Explicit
conversion [3]
•
What happens when you try to force a value
into a variable into which it won’t fit?
– Example: shortVar:
byte byteVar; 281
short shortVar = 281; byteVar: 25
byteVar = (byte)shortVar;
•
How can handle?
– [1] check the value of the source variable and
compare with the limits of the destination
checked()
variable
unchecked()
– 16
- Type conversion – Explicit
conversion [4]
• Example:
17
- Explicit conversions using the
Convert commands
18
- Enumeration (p.102)
•
An enumeration is a user-defined integer
type
•
Using: should use enum types any time you
need to represent a fixed set of constants
Using constants: Using enumeration:
•
Example:
final int RED = 1; enum trafficLights
final int YELLOW = 2; {
final int GREEN = 3; RED,
YELLOW,
GREEN
}
19
- Enumeration syntax
•
Defining enumeration
enum { By default, each value is assigned a
, corresponding value automatically
, according to the order in which it is
... defined, starting from zero. This means
that gets the value 0,
} gets 1 and so on
;
.
•
Declare variables of enum:
•
Access value of the enum:
20
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
ERROR:connection to 10.20.1.98:9315 failed (errno=111, msg=Connection refused)
ERROR:connection to 10.20.1.98:9315 failed (errno=111, msg=Connection refused)
Đang xử lý...