Câu lệnh
- Các câu lệnh được viết trong thân của phương thức (ở đây là phương thức Main)
- Thực hiện một công việc nào đó
- Kết thúc bởi dấu chấm phẩy (;)
Khoảng trắng
Bao gồm:
- Ký tự trắng, ký tự xuống dòng, ký tự tab.
- Dòng trống.
Cách viết khó đọc
|
namespace HelloWorld
{class Program
{
static void Main(string[] args)
{
Console.Write("Hello oktot.com!");
Console.ReadLine();}
}
|
Sử dụng hợp lý => chương trình dễ đọc.
|
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello oktot.com!");
Console.ReadLine();
}
}
}
|
Chú thích
- Chú thích (comment) được dùng để giải thích về chương trình và các câu lệnh.
- Giúp cho chương trình dễ hiểu hơn.
- Được bỏ qua khi biên dịch.
- Không ảnh hưởng tới kết quả thực thi của chương trình.
- Có thể phát sinh ra documentation của chương trình qua chú thích XML.
Hai cách tạo chú thích cơ bản
- Gõ phần chú thích sau cặp ký tự //
- Gõ phần chú thích giữa cặp ký tự /* và */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/* Chương trình C# đầu tiên
In ra câu chào "Hello oktot.com" */
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello oktot.com!"); // Xuất ra câu chào
Console.ReadLine(); // Chờ nhấn Enter
}
}
}
|
XML Comment
- Cho phép phát sinh ra sưu liệu dạng XML.
- Thích hợp cho việc viết sưu liệu của dự án lớn.
- Chú thích XML bắt đầu với triple slash (“///”) và các tag của XML.
- Chú thích XML dùng cho:
- User defined types.
- Class, delegate, enum and struct.
- Member of user defined types.
C# Code without XML Comment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using System;
namespace XMLCommentDemo
{
public class Temperature
{
public static int CelsiusToFahrenheit(int degreesCelsius)
{
return ((int)((9/5)*degreesCelsius) + 32);
}
public static int FahrenheitToCelsius(int degressFahrenheit)
{
return ((int)((5/9)*(degressFahrenheit - 32)));
}
}
}
|
C# Code with XML Comment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
using System;
namespace XMLCommentDemo{
/// <summary>
/// Class temperature provides functions which convert among various
/// temperature scales.
/// </summary>
public class Temperature {
/// <summary>
/// Converts degrees Celsius to degrees Fahrenheit
/// </summary>
/// <param name="degreesCelsius">Degrees Celsius</param>
/// <returns>Returns degrees Fahrenheit</returns>
public static int CelsiusToFahrenheit(int degreesCelsius) {
return ((int)((9/5)*degreesCelsius) + 32);
}
/// <summary>
/// Converts degrees Fahrenheit to degrees Celsius
/// </summary>
/// <param name="degressFahrenheit">Degrees Fahrenheit</param>
/// <returns>Returns degrees Celsius</returns>
public static int FahrenheitToCelsius(int degressFahrenheit) {
return ((int)((5/9)*(degressFahrenheit - 32)));
}
}
}
|
Chúc các bạn thành công!
Không có nhận xét nào:
Đăng nhận xét