一元操作符。只有一个操作数,如—x
二元操作符。有俩个操作数。x+y
三元操作数。c?x:y
int 整形 储存 32位有符号
long 长整形 64 有
short 短整型 16 有
sbyte 8 有
byte 字节型 8 位无符号
ushort 无符号短整型 16 无
uint 无符号整形 32 无
ulong 无符号长整形 64 无
float 单精度浮点型
double 双精度浮点型
decimal 10进制数字型 储存 28位10进制数字
char 字符型 储存unicode字符值
strint 字符串型 储存unicode字符数组
object 对象性
bool 布尔型
隐式转化: 当吧某种数据类型转化成另外一种数据类型,而不丢失任何数据结构时,这种转化
如果丢失数据,那么 。。。用显式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilityMethods;
namespace Test
{
class Program
{
public static void Main ()
{
Console.WriteLine(-8/3);
Console.WriteLine(8/3);
Console.WriteLine(7/3);
Console.WriteLine((8/3)==(7/3));
Console.WriteLine(8.0/3);
Console.WriteLine(7.0/3);
Console.WriteLine((8.0/3)==(7.0/3));
Console.Write("{0} ", 1 is int);
Console.Write("{0} ", 1 is float);
Console.Write("{0} ", 1.0 is float);
Console.WriteLine("{0} ", 1.0 is double);
short a = 10;
ushort b = 10;
int c = 10;
uint d = 10;
Console.Write("{0} ",~10); //~取补
Console.Write("{0} ",~a);
Console.Write("{0} ",~b); //ushort 0 到 65,535
Console.Write("{0} ",~c);
Console.WriteLine("{0} ", ~d); //uint 0 到 4,294,967,295
int x = 16;
Console.Write("{0} ",x);
int y = x >> 2;
Console.Write("{0} ",y);
y = y >> 2;
Console.Write("{0} ", y);
y = y >> 2;
Console.WriteLine("{0} ", y);
int x1 = 5;
int y1 = x1++;
Console.Write("x1={0},y1={1} ",x1,y1);
y1 = ++x1;
Console.Write("x1={0},y1={1} ",x1,y1);
Type[] t ={
typeof(int),typeof(System.Int32 ),typeof(string),typeof(double[])
};
for (int i = 0; i < t.Length; i++)
{
Console.Write("{0} ",t[i].Name);
}
Console.ReadKey();
}
}
}
运算结果。。
D:/microsoft/VC>delegate.exe
3
2
7
-1
D:/microsoft/VC>test1.exe -2 2 2 True 2.66666666666667 2.33333333333333 False True False False True -11 -11 -11 -11 4294967285 16 4 1 0 x1=6,y1=5 x1=7,y1=7 Int32 Int32 String Double[] |
Typeof操作符
Typeof操作符 用于获取系统原型对象的类型。
Type[] t ={
typeof(int),typeof(System.Int32 ),typeof(string),typeof(double[])
};
for (int i = 0; i < t.Length; i++)
{
Console.Write("{0} ",t[i].Name);
}
如上图所示
这表明 int和System.Int32是同一类型。。
三元操作符
“b?x:y” “?:”三元操作符,也叫条件操作符
先计算条件b,然后进行判断。如何b的值为真,那么计算x的值,运算结果为x的值;否则计算y的值。条件操作符是向右关联的,也就是说,它从左向右分组计算。例如表达式a?b:c?d:e
将按照a?b(c?d:e)
using System;
class MathClass
{
public static int max(int a,int b)
{
return(a>b?a:b);
}
public static int min(int a,int b)
{
return(a<b?a:b);
}
public static int sub(int a,int b)
{
return(a+b);
}
public static int minus(int a,int b)
{
return(a-b);
}
}
class Handler
{
private delegate int Calculation(int a,int b);
private static Calculation[] myCalculation=new Calculation[2];
public static void EventHandler(int i,int a,int b)
{
switch(i)
{
case 1:
myCalculation[0]=new Calculation(MathClass.max);
myCalculation[1]=new Calculation(MathClass.min);
Console.WriteLine(myCalculation[0](a,b));
Console.WriteLine(myCalculation[1](a,b));
break;
case 2:
myCalculation[0]=new Calculation(MathClass.sub);
myCalculation[1]=new Calculation(MathClass.minus);
Console.WriteLine(myCalculation[0](a,b));
Console.WriteLine(myCalculation[1](a,b));
break;
default:
return;
}
}
}
class Test
{
static void Main ()
{
Handler.EventHandler(1,2,3);
Handler.EventHandler(2,3,4);
Console.ReadKey();
}
}
D:/microsoft/VC>delegate.exe 3 2 7 -1 |
Checked和unchecked操作符
using System;
class MyTest
{
static int x=1000000;
static int y=1000000;
static int F()
{
return checked(x*y); //编译错误,抛出异常。
}
static int G()
{
return unchecked(x*y); //返回值为 —727379968
}
static int H()
{
return x*y; //依赖于编译时的默认情况。
}
}
checked(C# 参考)
checked 关键字用于对整型算术运算和转换显式启用溢出检查。
默认情况下,如果表达式产生的值超出了目标类型的范围,则常数表达式将导致编译时错误,而非常数表达式在运行时计算并将引发异常。不过,如果通过编译器选项或环境配置在全局范围内取消了溢出检查,则可以使用 checked 关键字来启用此项功能。
请参见有关 unchecked 关键字用法的 unchecked 示例。
示例
此示例演示如何对非常数表达式使用 checked。在运行时会报告溢出。
|
class OverFlowTest {
static short x = 32767; // Max short value static short y = 32767; // -32,768 +32,767
// Using a checked expression static int CheckedMethod() { int z = 0; try { z = checked((short)(x + y)); } catch (System.OverflowException e) { Console.WriteLine(e.ToString()); } return z; }
static void Main () { Console.WriteLine("Checked output value is: {0}", CheckedMethod()); } } /* Output: System.OverflowException: Arithmetic operation resulted in an overflow. at OverFlowTest.CheckedMethod() Checked output value is: 0
*/ |
D:/microsoft/VC>check.exe System.OverflowException: 算术运算导致溢出。 在 OverFlowTest.CheckedMethod() Checked output value is: 0
D:/microsoft/VC> |
D:/microsoft/VC>check.exe
System.OverflowException: 算术运算导致溢出。
在 OverFlowTest.CheckedMethod()
Checked output value is: 0
D:/microsoft/VC>
unchecked
class TestClass
{
const int x = 2147483647; // Max int
const int y = 2; //
static void Main ()
{
int z;
unchecked
{
z = x * y;
}
Console.WriteLine("Unchecked output value: {0}", z);
}
}
// Output: Unchecked output value: -2