Bài 02: Cấu trúc một chương trình C# (Csharp)

0

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.net!");
       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.net!");
            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ự /**/
/* Chương trình C# đầu tiên
   In ra câu chào "Hello oktot.net" */

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Hello oktot.net!"); // 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

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

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)));
    }
  }
}

Xem thêm Bài 03: Cú pháp C# (Csharp) cơ bản

Chúc các bạn thành công!