C# 语法练习(3): 运算符
2009-02-23 08:17:05 来源:WEB开发网++ -- 可以对 double 类型
using System;
class MyClass
{
static void Main()
{
double f = 1.5;
f++; Console.WriteLine(f); //2.5
f--; Console.WriteLine(f); //1.5
Console.WriteLine(++f); //2.5
Console.WriteLine(--f); //1.5
Console.ReadKey();
}
}
?:
using System;
class MyClass
{
static void Main()
{
int n, a=11, b=22;
n = a > b ? a : b;
Console.WriteLine(n); //22
n = a < b ? a : b;
Console.WriteLine(n); //11
Console.ReadKey();
}
}
??
using System;
class MyClass
{
static void Main()
{
int? x = null; /* 给变量赋 null 的写法, 一般用于数值和布尔类型 */
int y;
y = x ?? -1; /* 如果 x 为 null 将返回后者, 反之返回 x */
Console.WriteLine(y); // -1
x = 9;
y = x ?? -1;
Console.WriteLine(y); // 9
Console.ReadKey();
}
}
=>
using System;
using System.Linq;
class MyClass
{
static void Main()
{
int n1, n2;
int[] ns = {22, 333, 4444, 9};
n1 = ns.Max(num => num);
n2 = ns.Min(num => num);
Console.WriteLine("{0}, {1}", n1, n2); //4444, 9
string[] ss = {"aaa", "bbbb", "ccccc", "dd"};
n1 = ss.Max(str => str.Length);
n2 = ss.Min(str => str.Length);
Console.WriteLine("{0}, {1}", n1, n2); //5, 2
Console.ReadKey();
}
}
更多精彩
赞助商链接