checked, unchecked:
unchecked 关键字用于取消整型算术运算和转换的溢出检查。在未检查的上下文中,如果表达式产生目标类型范围之外的值,则结果被截断。默认情况下,启用溢出检测,这与使用 checked 具有相同的效果。
public int UncheckedAdd(int a, int b)
{
return unchecked(a + b);
}
unsafe, fixed:
fixed 关键字 用来防止garbage collector 重定向一个可以移动的变量,fixed关键字只能用在unsafe上下文或者固定大小缓冲区的创建上。例子:
class FixedTest
{
// Unsafe method: takes a pointer to an int.
unsafe static void SquarePtrParam(int* p)
{
*p *= *p;
}
unsafe static void Main()
{
Point pt = new Point();
pt.x = 5;
pt.y = 6;
// Pin pt in place:
fixed (int* p = &pt.x)
{
SquarePtrParam(p);
}
// pt now unpinned
Console.WriteLine("{0} {1}", pt.x, pt.y);
}
}
Typeof, is,as:
typeof用于获取类型的 System.Type 对象。typeof 表达式采用以下形式:
System.Type type = typeof(int);
is 关键字用于判断某实例是不是属于某一类型,如:
if (obj is string)
{
}
as 用来做兼容类型的转换,和强制类型转换类似,只是如果转换失败,会返回null,不会抛出异常,例如:
string s = someObject as string
Array:
数组表示方法在C#和C语言中并不相同。
int[] myInts = { 5, 10, 15 };(C#)
int prime64Array[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61} (C++)
多维数组的表示方法为:
int[,] ArrayMaze = new int[3, 4] { {2,6,7,2},
{6,8,43,2},
{1,4,6,0}};
Interface, abstract, virtual的区别:
interface用来声明接口
只提供一些方法规约,不提供方法主体,interface的方法不能用public abstract等修饰,无字段变量,无构造函数。可包含参数,实现必须实现interface中的各个方法。
abstract声明抽象类、抽象方法
1.抽象方法所在类必须为抽象类
2.抽象类不能直接实例化,必须由其派生类实现。
3.抽象方法不包含方法主体,必须由派生类以override方式实现此方法,这点跟interface中的方法类似。
virtual标记方法为虚方法
1.可在派生类中以override覆盖此方法
2.不覆盖也可由对象调用
3.无此标记的方法(也无其他标记),重写时需用new隐藏原方法
abstract与virtual: 方法重写时都使用 override 关键字
interface中的方法和abstract方法都要求实现
C# 中的数据类型
简单类型 | ||
数据类型 | 长度 | 取值范围(特性) |
sbyte | 8 | -128~127 |
byte | 8 | 0~255 |
short | 16 | -32768~32767 |
ushort | 16 | 0~65535 |
int | 32 | -2147483648~2147483647 |
uint | 32 | 0~4294967295 |
long | 64 | -9223372036854775808~9223372036854775807 |
ulong | 64 | 0~18446744073709551615 |
bool |
| true, false |
float |
| +-1.5×10-45~3.4×1038精度7位 |
double |
| +-5.0×10-324~1.7×10308精度15到16位 |
decimal | 128 | 1.0×10-28~7.9×1028 精度28到29位,使用m下标表示 ,如: decimal d_value = 1.0m; |
char | 16 | Unicode,如字符 ‘A’,16进制’/x0032’, Unicode’/u0032’; |
struct |
| 成员可以有访问权限,如public,可以有结构成员。 |
enum |
| 元素仅限于整型。 |
引用类型 | ||
数据类型 | 特性,例子 | |
class | 包含数据成员、函数成员、嵌套类型的数据结构。数据成员有常量、域、事件,函数成员有方法、属性、索引指示器、运算符、构造、析构函数。object类是所有类的基类。 | |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
反射
Reflection,中文翻译为反射。
这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’、‘模块(Module)’、‘类型(class)’组成,而反射提供一种编程的方式,让程序员可以在程序运行期获得这几个组成部分的相关信息,Assembly类可以获得正在运行的装配件信息,也可以动态的加载装配件,以及在装配件中查找类型信息,并创建该类型的实例。例子
System.Reflection.Assembly ass;
Type type ;
object obj;
try
{
ass = System.Reflection.Assembly.LoadFile(@"d:/TestReflect.dll");
type = ass.GetType("Webtest.ReflectTest");//必须使用名称空间+类名称
System.Reflection.MethodInfo method = type.GetMethod("WriteString");//方法的名称
obj = ass.CreateInstance("Webtest.ReflectTest");//必须使用名称空间+类名称
string s = (string)method.Invoke(obj,new string[] {"jianglijun"}); //实例方法的调用
static class
静态类声明表示其所有的成员都是静态的,不能用 new来实例化一个新的实例,不能被封装(sealed),不能有构造函数,主要用在不通过实例访问成员的情况。http://msdn2.microsoft.com/en-us/library/79b3xss3(VS.80).aspx
internal class, protected, public, private
没有嵌套在其他类或结构中的类和结构可以是公共的,也可以是内部的。声明为公共的类型可由任何其他类型访问。声明为内部的类型只能由同一程序集中的类型访问。类和结构默认声明为内部的,除非向类定义添加了关键字 public,通过组合 protected 和 internal 关键字,可以将类成员标记为受保护的内部成员 -- 只有派生类型或同一程序集中的类型才能访问该成员。最后,可以使用 private 关键字将类成员或结构成员声明为私有的,指示只有声明该成员的类或结构才能访问该成员。
访问权限关键字 | 含义 |
public | Access is not restricted. |
protected | Access is limited to the containing class or types derived from the containing class. |
internal | Access is limited to the current assembly. |
protected internal | Access is limited to the current assembly or types derived from the containing class. |
private | Access is limited to the containing type. |
sealed class
封闭类是防止被其他类继承而设置的类修饰符,如果试图将一个封闭类作为其他类的基类,将会有报错信息。封闭类不能是抽象类。Sealed修饰符不能用来修饰函数,但是可以在派生类的重写函数上使用sealed来防止它不被继续重写。如
class MyClass : MyClass1
{
public override sealed void Method()
{
Console.WriteLine("sealed method");
}
}
ConfigurationManager
ConfigurationManager是用来替代ConfigurationSettings类来进行当前程序的配置文件的读写操作的。配置文件是和assembly同名的.config文件。例子
//打开配置文件
configuration config = webconfigurationmanager.openwebconfiguration("~");
//获取appsettings节点
appsettingssection appsection = (appsettingssection)config.getsection("appsettings");
//在appsettings节点中添加元素
appsection.settings.add("addkey1", "key1s value");
appsection.settings.add("addkey2", "key2s value");
config.save();
IDisposable
IDisposable接口用来定义一个释放分配未托管资源的函数。垃圾回收器会自动释放托管资源的对象,但是无法预计什么时候GC会运行,另外,GC没有办法得到未托管资源的信息,比如Windows句柄,文件句柄或者流。这个接口的Dispose函数可以明确释放未托管资源。例如:
// 实现 IDisposable.
// 不要声明成虚函数.
// 派生类不能覆盖此函数.
public void Dispose()
{
Dispose(true);
// 此对象将会被Dispose函数释放.
// 因此,应该调用 GC.SupressFinalize 把这个对象
// 从 finalization 队列当中拿掉从而防止finalization
// 再次释放这个对象
GC.SuppressFinalize(this);
}
进程间通信和线程间通信
进程是一个具有独立功能的程序,他是关于某个数据集合的一次可以并发执行的运行活动。进程作为构成系统的基本细胞,不仅是系统内部独立运行的实体,而且是独立竞争资源的实体。
线程也被称为轻量级进程,同一进程的线程共享全局变量和内存,使得线程之间共享数据很容易也很方便,但会带来某些共享数据的互斥问题。
许对程序为了提高效率也都是用了线程来编写。
父子进程的派生是非常昂贵的,而且父子进程的通讯需要ipc或者其他方法来实现,比较麻烦。而线程的创建就花费少得多,并且同一进程内的线程共享全局存储区,所以通讯方便。
线程的缺点也是由它的优点造成的,主要是同步、异步和互斥的问题,值得在使用的时候小心设计。
下面的例子是在同一个类的不同线程之间对于全局变量的同步所做的锁操作:
public class AnazlyerUUIDJob
{
//check if the Merge Job is Running
private int running = 0; //0: false; 1: true.
public bool Running
{
…
public void ResetMergeJob()
{
//delete temp files
//enable key
System.Threading.Interlocked.Exchange(ref this.running, 0);
}
System.Threading.Interlocked 类为多线程对于公共成员访问提供原子操作。