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

Toán tử một ngôi (Unary Operators) trong C#

Các toán tử một ngôi trong C#:

+ - ! ~ ++ -- (type)* & sizeof

Tìm hiểu toán tử sizeof. Toán tử sizeof trả về kích thước kiểu dữ liệu.

Giả sử cần kích thước int:

sizeof(int)

Với double:

sizeof(double)

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

Ví dụ

using System; 
namespace Demo { 
    class Program { 
        static void Main(string[] args) { 
            Console.WriteLine("The size of int is {0}", sizeof(int)); 
            Console.WriteLine("The size of char is {0}", sizeof(char)); 
            Console.WriteLine("The size of short is {0}", sizeof(short)); 
            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 int is 4
The size of char is 2
The size of short is 2
The size of long is 8
The size of double is 8