资深C++程序员学习C#要点

来源:互联网 发布:手机淘宝尺寸怎么设置 编辑:程序博客网 时间:2024/06/04 18:11
1. 不转义字符串的格式
string strA = @"c:\new.test";
string strB = "c:\\new.test";
在这里,上面两个字符串等同,增加了@的使用,特殊含义。


2. 类型转换使用类Convert。


3. 关键字checked和unchecked
byte test = checked((byte)source);


4. 枚举类型
enum <typeName> : <underlyingType>
{
<value1> = <val1>, 
<value2> = <val2>,
...
}


underlyingType:可以指明为byte, sbyte, short, ushort, int, uint, long和 ulong。


5. 结构类型
struct route
{
public orientation direction;
public doubledistance;
}
结构有访问限定符。


6. 声明数组
<baseType>[] <name>;


int[] myArray = {1, 2, 3, 4, 5};
int[] myB = new int[5];
int[] myB = new int[5]{1, 2, 3, 4, 5}; //个数与分配数量等同。


7. foreach循环来访问数组
foreach (<baseType> <name> in <array>)
{
//使用<name>
}


8. 多维数据
二维数组: int[,] testA = new int[3,4]
double[,] two = { {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}};


9. params关键字


10. ref引用参数


11. out关键字


12. delegate关键字指明函数变量
声明函数变量:
delegate int Fun(int p1, int p2);
Fun f;
f = new Fun(otherFun);
f(1, 2);




13. 输出调试信息
Debug.WriteLine()
Trace.WriteLine()


Debug.Assert()
Trace.Assert()


14. 自动删除对象用法
using (<ClassName> <VariableName> = new <ClassName>())
{
...
}


15. abstract只能抽象,不能实例化,只能继承;sealed刚好相反。


16. interface定义接口,接口没有继承System.Object,不能实例化。


17. internal只有项目内部代码访问。


18. readonly只读属性
public readonly int myInt = 17;


19. 定义属性的语法
private int myInt;


//
public int Myint
{
get
{
return myInt;
}
set
{
}
}




20. 使用集合System.Collections


21. 使用迭代器语句yield


22. is运算


23. as运算符,把一种类型转换为指定的引用类型
ClassD obj2 = obj1 as ClassD;


24. 事件处理
public event EventHandler M;


25. var关键字使用


蔡军生  QQ:9073204  深圳




















0 0