C#图解教程读书笔记(第8章 表达式和运算符)

表达式

字面量

整数字面量

字符字面量

字符串字面量

求值顺序

优先级

结合性

与C和C++不同,在C#中的数字不具有布尔意义。

各种运算符的作用(过)

用户定义类型转换

class XiXiInt

{

const int iMaxValue = 100;

const int iMinValue = 0;

private int theValue = 0;

public int TheValue

{

get { return theValue; }

set

{

if (value < iMinValue)

{

theValue = iMinValue;

}

else

{

theValue = value > iMaxValue ? iMaxValue : value;

}

}

}

public static implicit operator int(XiXiInt iXiXi)

{

return iXiXi.TheValue;

}

public static implicit operator XiXiInt(int x)

{

XiXiInt iXiXi = new XiXiInt();

iXiXi.TheValue = x;

return iXiXi;

}

}

显式转换和强制转换运算符

如果将implicit改为explicit的话,则需要显式强制转换。

 

运算符重载

运算符重载只能用于类和结构。

 

原文地址:https://www.cnblogs.com/pengjia89/p/3663755.html