目录

Show 例子

还有其他一些重要的运算符,包括sizeof? : ? :由C#支持。

操作者 描述
sizeof() 返回数据类型的大小。 sizeof(int), returns 4.
typeof() 返回类的类型。 typeof(StreamReader);
& 返回变量的地址。 &一个; 返回变量的实际地址。
*Pointer to a variable. *一个; 为变量创建名为“a”的指针。
? : 条件表达式 如果条件为真? 然后是值X:否则为Y值
is 确定对象是否属于某种类型。 如果(福特是汽车)//检查福特是否是汽车类的对象。
as 如果演员表失败,则不会引发异常。 Object obj = new StringReader(“Hello”);

StringReader r = obj as StringReader;

例子 (Example)

using System;
namespace OperatorsAppl {
   class Program {
      static void Main(string[] args) {
         /* example of sizeof operator */
         Console.WriteLine("The size of int is {0}", sizeof(int));
         Console.WriteLine("The size of short is {0}", sizeof(short));
         Console.WriteLine("The size of double is {0}", sizeof(double));
         /* example of ternary operator */
         int a, b;
         a = 10;
         b = (a == 1) ? 20 : 30;
         Console.WriteLine("Value of b is {0}", b);
         b = (a == 10) ? 20 : 30;
         Console.WriteLine("Value of b is {0}", b);
         Console.ReadLine();
      }
   }
}

编译并执行上述代码时,会产生以下结果 -

The size of int is 4
The size of short is 2
The size of double is 8
Value of b is 30
Value of b is 20
↑回到顶部↑
WIKI教程 @2018