7.三元操作符
三元操作符(?:)也称条件操作符。对条件表达式"b?x:y",总是先计算条件b,然后进行判断。如果b的值为true,则计算x的值,否则计算y的值。条件运算符为右联运算符,因此该形式的表达式 a ? b : c ? d : e 按如下规则计算:a ? b : (c ? d : e)
8. . 运算符
点运算符用于成员访问。name1 . name2
| class Simple { public int a; public void b() { } } Simple s = new Simple(); //变量 s 有两个成员 a 和 b;若要访问这两个成员,请使用点运算符 s.a = 6; // assign to field a; s.b(); // invoke member function b; |
| using System; class MikeCat { public static void Main() { double x,y; x=1.5; Console.WriteLine(++x);//自增后等于2.5 y=1.5; Console.WriteLine(y++);//先显示1.5后自增 Console.WriteLine(y);//自增后等于2.5 } } |
| using System; class MyClass1 { } class MyClass2 { } public class IsTest { public static void Main() { object [] myObjects = new object[6]; myObjects[0] = new MyClass1(); myObjects[1] = new MyClass2(); myObjects[2] = "hello"; myObjects[3] = 123; myObjects[4] = 123.4; myObjects[5] = null; for (int i=0; i<myObjects.Length; ++i) { string s = myObjects[i] as string; Console.Write ("{0}:", i); if (s != null) Console.WriteLine ( "'" + s + "'" ); else Console.WriteLine ( "not a string" ); } } } 输出 0:not a string 1:not a string 2:'hello' 3:not a string 4:not a string 5:not a string |

