Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Toán tử sizeof trong C# có công dụng gì?

Toán tử sizeof() trả về kích thước của một kiểu dữ liệu. Giả sử cần tìm kích thước kiểu int:

sizeof(int);

Với kiểu double:

sizeof(double);

Ví dụ hoàn chỉnh tìm kích thước các kiểu dữ liệu:

Ví dụ

using System; 
namespace Demo { 
    class Program { 
        static void Main(string[] args) { 
            Console.WriteLine("The size of long is {0}", sizeof(long)); 
            Console.WriteLine("The size of double is {0}", sizeof(double)); 
            Console.ReadLine(); 
        } 
    } 
}

Kết quả

The size of long is 8
The size of double is 8