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. | 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 Variables - Constants - Expressions More about variables Flow control Methods Using some classes Exception Ebook: from chapter 1 to 6 (Part I) 3 Variables, Constants Declare variable: example: int x, y; note: Variables need to be initialized before it’s used Declare constant: example: const double PI = ; Data types that are built into C# 14 primitive data types: string, int, double, char, long, (see next table) DataType name; DataType name = init_value; const DataType CONST_NAME = value; 4 Khai bao bien voi var Primitive Data Types 5 8 kiểu số nguyên 3 kiểu số thực char: Single Unicode character, stored as an integer between 0 and 65535 short ushort int uint long ulong float string: ko có giới hạn số ký tự, tùy thuộc vào bộ nhớ 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 Example: Right variable names: myBigVar VAR1 _test Wrong variable names: 99BottlesOfBeer namespace 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 Mục tiêu: đặt tên (variables, subprograms, functions, tables, fields, queries, reports etc) đủ nghĩa, ko quá dài, ko quá ngắn đến vô nghĩa (vd/ m,n) nhằm tạo sự thống nhất trong nhóm lập trình và dễ bảo trì sau này | 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 Variables - Constants - Expressions More about variables Flow control Methods Using some classes Exception Ebook: from chapter 1 to 6 (Part I) 3 Variables, Constants Declare variable: example: int x, y; note: Variables need to be initialized before it’s used Declare constant: example: const double PI = ; Data types that are built into C# 14 primitive data types: string, int, double, char, long, (see next table) DataType name; DataType name = init_value; const DataType CONST_NAME = value; 4 Khai bao bien voi var Primitive Data Types 5 8 kiểu số nguyên 3 kiểu số thực char: Single Unicode character, stored as an integer between 0 and 65535 short ushort int uint long