C# 中的一元运算符有哪些?


以下是 C# 中的一元运算符 −

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

让我们来了解一下 sizeof 运算符。sizeof 会返回一种数据类型的长度。

假设你需要找出 int 数据类型的长度 −

sizeof(int)

对于 double 数据类型 −

sizeof(double)

让我们来看一个完整的示例来找出各种数据类型的长度 −

示例

 直播演示

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

输出

The size of int is 4
The size of int is 2
The size of short is 2
The size of long is 8
The size of double is 8

更新于: 20-6 月-2020

308 次浏览

开启你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.