Một biến là chứa các thông tin, mà có thể thay đổi giá trị của nó.
Nó cung cấp nghĩa cho:
- Lưu trữ thông tin;
- Lấy các thông tin được lưu trữ;
- Sửa đổi các thông tin được lưu trữ.
e.g. Khởi tạo biến a = 3; và có thể thay đổi trong quá trình thực thi chương trình.
Kiểu dữ liệu?
Kiểu dữ liệu là một dải dữ liệu có đặc trưng tương tự nhau.
Với kiểu Byte xác định một tập hợp số nguyên trong khoảng [0..255]
Đặc trưng (Characteristics)
Các kiểu dữ liệu được đặc trưng bởi:
- Name: int;
- Size (how much memory they use) - 4 bytes;
- Default value - 0.
Các kiểu
Trong c#, Các kiểu dữ liệu cơ bản được phân thành các kiểu dữ liệu sau đây:
- Integer types: sbyte, byte, short, ushort, int, uint, long, ulong;
- Real floating-point types: float, double;
- Real type with decimal precision: decimal;
- Boolean type: bool;
- Character type: char;
- String: string;
- Object type: object.
|
Data Types
|
Default Value
|
Minimum Value
|
Maximum Value
|
|
sbyte
|
0
|
-128
|
127
|
|
byte
|
0
|
0
|
255
|
|
short
|
0
|
-32768
|
32767
|
|
ushort
|
0
|
0
|
65535
|
|
Int
|
0
|
-2147483648
|
2147483647
|
|
uint
|
0u
|
0
|
4294967295
|
|
long
|
0L
|
-9223372036854775808
|
9223372036854775807
|
|
ulong
|
0u
|
0
|
18446744073709551615
|
|
float
|
0.0f
|
±1.5×10-45
|
±3.4×1038
|
|
double
|
0.0d
|
±5.0×10-324
|
±1.7×10308
|
|
decimal
|
0.0m
|
±1.0×10-28
|
±7.9×1028
|
|
bool
|
false
|
Two possible values: true and
false
|
|
|
char
|
‘\u0000’
|
‘\u0000’
|
‘\uffff’
|
|
object
|
null
|
-
|
-
|
|
string
|
null
|
-
|
-
|
Characteristics of variables
- name (identifier): age
- type (các thông tin lưu trữ trong nó): int;
- value (thông tin được lưu trữ): 25
Một biến là một vùng được đặt tên của bộ nhớ
Biến được lưu trữ trực tiếp trong bộ nhớ của chương trình (stack)
or trong bộ nhớ động mà các đối tượng lớn được lưu trữ (character string and arrays)
Primitive data types (number, char, bool) are called value types because they store their value directly in the program stack.
Reference data types (string, object, array) are an address, pointing to the dynamic memory where their value is stored. They can be dynamic allocated and released.
Naming variables - Rules
- Variable names can contain the letters a-z, A-Z, the digits 0-9 as well as the character '_'
- Variable names cannot start with a digit.
- Variable names cannot coincide with a keyword.
Naming variables - Recommendations
- The name should be descriptive and explain what the variable is used for.
e.g. an appropriate name for a variable storing a person's name ís personName and inappropriate name is a25.
- Only Latin characters should be used.
- In C#. the variable names should start with a small letter and include small letter, every new word, however, starts with capital letter.
e.g. firstName is correct and better to use than firstname or first_name;
- Variable names should be neither too long or too short.
- Case sensivity
Declare Variables
<data type> <identifier> [= <initialízation>];
e.g.
string name;
int age = 25;
Value and Reference types
0 nhận xét:
Đăng nhận xét