- 参考书: 《C# school 》
- C#中有两种数据类型:value type (固有数据类型,struct, enumeration); reference type (object, deleagte)
传参时,value type 是用拷贝,reference type只传reference (handle)
- C#中的一些固有数据类型:
char: 2 bytes
bool: 1 byte
decimal: 12 bytes
byte: 1 byte
sbyte: 1byte (-128 ~ 127)
ushort / short: 2 bytes
uint / int: 4 bytes
ulong / long: 8 bytes
float: 4 bytes
double: 8 bytes
- C#中 local variables 必须在使用前被初始化
static void Main()
{
int age;
// age = 18; // 这句必须放开下面才不报错
Console.WriteLen(age); // error
}
- 微软建议 变量名用 Camel Notation,即首字母小写,方法名用Pascal Notation,即首字母大写,如:
salary totalSalary;
GetTotal()
- 《C# school 》第53页提到,object在heap上创建,而implicit type在stack上创建
- object都是通过 new 的方式创建的,不能像c++中那样,声明一个class的对象(不是指针),示例:
Student theStudent = new Student();
或:
Student theStudent;
theStudent = new Student();
如果是固有类型,就不需要 new 操作:
int i;
i = 4;
注意
Student theStudent;
这里的theStudent只是声明一个reference,没有任何实际内容。直接使用未做new操作的object reference会报错“unassigned”
- 默认值:
int / long: 0
float / double: 0.0
bool: False
char: '\0'
string: ""
Object: null
- 打印示例:
Console.WriteLine("Current time is: " + DateTime.Now);
- 类的Access Modifier

其中的private,protected,public,功能和c++一致;protected internal, internal 是 C++ 中没有的。
internal 访问权限范围是:当前project
protected internal 访问权限范围是:当前project,或者子类
- C# 中 struct 和 class 的异同:
struct 是 value type,而 class 是 reference type
struct 和 class 都可以有 constructor,数据成员,成员函数
struct 不能从别的 class 派生,也不能被别的类 继承
struct 可以实现 interface
和 C# 中其他固有type一样,struct 也是隐式地从 System.Object 派生而来
struct 的实例可以用 new 创建,也可以不用 new 创建
由于 size 较小,系统在使用 struct 的时候,比使用 class 效率更高
- 重载了Object.ToString()方法的 class 或 struct,其实例(例如 pt )放在Console.WriteLine() 中的时候,不需要用 pt.ToString() 的形式,直接 Console.WriteLine(pt) 就可以了
- C# 不要求 class 显示地从 System.Object 派生,因为该派生是自动的,隐式的。事实上,.Net framework 中所有的 class 都从System.Object 派生,System.Object 中定义的所有方法在系统中都是可用的,派生类可以重载 System.Object 的一些方法,包括
Equals - 用于 objects 之间的比较
Finalize - 当一个Object 被自动回收时,执行 cleanup 操作
GetHashCode - Generates a number corresponding to the value of the object to support the use of a hash table
ToString - 生成描述 class 的可读文本
- C#注释有三种格式:
1) //注释内容
2) /*注释内容*/
3) XML注释功能,例如:
/// <summary>/// The main entry point for the application.
/// </summary>
参考:http://www.cnblogs.com/wj110reg/articles/845944.html
- C#和.Net framework的关系:
C#程序在.Net framework平台上执行,后者含有Common Language Runtime (CLR) 的虚拟执行系统。CLR 是 Microsoft 的 Common Language Infrastructure (CLI) ,可以使得不同的编程语言和开发环境之间,合作无间。
用C#写的原始程序遵循CLI规则的中继语言(IL),IL程序以及图片,字符串等资源都被存储在.exe或.dll的文件中。执行C#程序时,IL程序及相关资源会被载入CLR,然后由CLR编译,将IL指令转成本机指令并执行。以下是示意图:

由于C#编译产生的IL程序遵循CTS规格,所以C#程序可以喝Visual Basic.Net, Visual C++, Visual J#等20多种遵循CTS规格的语言产生的代码互动。
参考:http://msdn.microsoft.com/zh-tw/library/z1zx9t92%28v=vs.80%29.aspx
本文详细介绍了C#编程的基础知识,包括数据类型、变量使用、类的访问修饰符、struct与class的区别、注释方式、与.NET框架的关系及运行机制。重点突出C#在开发过程中的应用技巧和注意事项。

3万+

被折叠的 条评论
为什么被折叠?



