c# 学习总结

基本数据类型

类型 符号位 取值 默认值 说明
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 ();//r等于3
	}
	
	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 ();//r等于4
	}
	
	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 提供的参数
  • 逆变、协变(in、out)
//逆变
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();//抛出ThreadAbortException异常,不能被捕获,可以进入finally块。
  • 锁:
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);//初始0,最大5

static void Main(string[] args){
	a.WaitOne();//每一个线程进来就减1,减到0后,后面的线程就阻塞了。
	...
	a.Release();//每次调用加1,也可以a.Release(n)加n。
}
发布了41 篇原创文章 · 获赞 4 · 访问量 3905

猜你喜欢

转载自blog.csdn.net/weixin_42487874/article/details/103028842