基本数据类型
类型 |
位 |
符号位 |
取值 |
默认值 |
说明 |
bool |
8 |
无 |
True\False |
Flase |
|
byte |
8 |
无 |
|
0 |
|
sbyte |
8 |
有 |
|
0 |
|
char |
16 |
无 |
|
‘\0’ |
unicode |
short |
16 |
有 |
|
0 |
|
ushort |
16 |
无 |
|
0 |
|
int |
32 |
有 |
|
0 |
|
uint |
32 |
无 |
|
0 |
|
long |
64 |
有 |
|
0 |
|
ulong |
64 |
无 |
|
0 |
|
float |
32 |
有 |
|
0 |
单精度浮点数 |
double |
64 |
有 |
|
0 |
双精度浮点数 |
运算符优先级
从上到下、从左往右 |
() [] . -> |
+(正) -(负) ++ – (type) * & ! ~ sizeof |
* / % |
+ - |
<< >> |
< <= > >= |
== != |
&(与) |
^(异或) |
(或) |
&&(并且) |
(或者) |
?: |
= += -= *= /= %= |
, |
常用关键字
sizeof(type):
获得类型大小。
typeof(type):
获得System.Reflection.MemberInfo类型。
const:
常量。
as:
类型转换,失败不会抛出异常。
is:
判断对象是否为某一类型。
??:
{a = b ?? c;}如果b不为null就返回b,否之返回c。b可以是可空类型或者引用类型。
值类型
- 基本数据类型。
结构体:
不能定义空参构造函数,不能继承结构体,成员不能为protected、abstract
、vertual。
- 枚举。
- 可空类型:用{int?a = null;}用这种形式定义的类型,取值范围是原来的范围再加一个null。
引用类型
- object
- dynamic
- string
- string str = “abc”;
- string str = @“abc”;忽略转义字符,可任意换行。
- class
- interface
- delegate
类
[abstract][sealed][internal\public] class [类名] : [类、接口]{
[internal\public\protected\private(默认)] [type] [变量名];
public [类名](参数):base(参数){}
[internal\public\protected\private] [abstract] [返回值类型] [函数名]();
[public\protected\private] [virtual] [返回值类型] [函数名](){}
[public\protected\private] override [返回值类型] [父类虚函数名](){}
}
abstract class A{
public abstract void func();
}
public class B : A{
public override void func(){}
}
public class A{
public virtual void func(){}
}
public class B : A{
public override void func(){}
}
接口
interface [类名] : [接口1,接口2 ...]{
void func();
}
interface A{
void func();
}
public class B : A{
public void func(){}
}
委托
- 定义:public delegate [返回值类型] [委托名] (参数列表);
- 绑定:
- 单播: [委托名] [变量名] = new [委托名] (函数名);
- 多播:
- 添加:[委托名] [变量名] += new [委托名] (函数名);
- 删除:[委托名] [变量名] -= new [委托名] (函数名);
- 调用:[变量名] (参数列表);
注意: 多播将调用所有绑定的函数,如果有返回值,则返回之后一个函数的返回值。
- 匿名委托:delegate(参数){};
public class A{
public delegate int Handler();
public A(){
Handler handler = func1;
handler += func2;
handler += delegate(){ return 3; };
int r = handler ();
}
public int func1(){
return 1;
}
public int func2(){
return 2;
}
}
事件
- 定义:event [委托名] [变量名]
- 添加:[变量名] += [函数名]
- 删除:[变量名] -= [函数名]
- 调用:[变量名] (参数类表)
public class A{
public delegate int Handler();
public event Handler m_event;
public A(){
m_event += func1;
m_event += func2;
m_event += delegate(){ return 4; };
int r = m_event ();
}
public int func1(){
return 1;
}
public int func2(){
return 2;
}
}
泛型
泛型类型 |
定义 |
泛型结构体 |
struct Name< T > |
泛型类 |
class Name< T > |
泛型接口 |
interface Name< T > |
泛型函数 |
返回类型 Func< T > (T) |
泛型委托 |
delegate 返回类型 委托名< T > (T) |
约束类型 |
说明 |
where T : struct |
T必须为struct |
where T : class |
T必须为class |
where T : interface |
T必须为interface |
where T : 基类 |
T必须为基类或者继承自基类 |
where T : new() |
T必须有无参构造函数 |
where T : U |
为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数 |
class A : IList<in T>{}
A<int> a1 = new A<int>();
A<object> a2 = new A<object>();
a1 = a2;
class A : IList<out T>{}
A<int> a1 = new A<int>();
A<object> a2 = new A<object>();
a2 = a1;
部分类
- 部分类:可以把一个类拆成多个部分在不同文件里定义。
public partial class A{
public void func1(){}
}
public partial class A{
public void func2(){}
}
static void Main(string[] args){
A a = new A();
a.func1();
a.func2();
}
特性
- [AttributeUsage(AttributeTarget, AllowMultiple, )] 用来修饰类的。
- AttributeTarget:使用范围。
- AllowMultiple:多用途。
- 是否能继承。
- [Conditional(“宏定义”)] 用来条件编译的。
- [Obsolete(“输出信息”, 是否报错)] 用来修饰过时的方法的。
- 用户自定义
[AttributeUsage(AttributeTarget.class | AttributeTarget.Method, true, true)]
public class A : System.Attribute{
int m_a;
string m_b;
public A(int a, string b){
m_a = a;
m_b = b;
}
}
[A(1, "abc")]
class B{
[A(2, "asdgf")]
public void func(){}
}
线程
Thread t = new Thread([函数名]);
t.Start();
t.Sleep();
t.Abort();
A a = new A();
static void Main(string[] args){
lock(a){
...
}
}
- Monitor:
- Monitor.Enter(object);
- Monitor.TryEnter(object);可以防止死锁。
- Monitor.Exit(object);
- 互斥体:
readonly Mutex m = new Mutex();
static void Main(string[] args){
m.WaitOne();
...
m.ReleaseMutex();
}
ReaderWriteLockSlim a = new ReaderWriteLockSlim ();
static void Main(string[] args){
a.EnterReadLock();
...
a.ExitReadLock();
a.EnterWirteLock();
...
a.ExitWriteLock();
}
主义:不要用ReaderWriteLock,有BUG。
Semaphore a = new Semaphore(0,5);
static void Main(string[] args){
a.WaitOne();
...
a.Release();
}