c# 随笔

来源:互联网 发布:阿里云 idc报告 编辑:程序博客网 时间:2024/05/19 12:15

匈牙利标记法、camel命名法、Pascal命名法

decimal decimal coin = 0.42M
以*.sln为打开链接,一个.sln可以包含多个.csproj
infinity 无穷大 0*infinity=0
NaN not a number
0*NaN=NaN
NaN*infinity=infinity
组件 component

optMethod(first:99,third:"world");optMethod(99,third:"world");

试图引用没有初始化的数据不能通过编译
C#里面没有指针的概念

结构和类的主要区别

问题 结构 类 是值类型还是引用类型? 结构是值类型 类是引用类型 它们的实例是存储在栈(stack)上还是堆(heap)上? 结构的实例称为值,存储在栈(stack)上 类的实例称为对象,存储在堆(heap)上 可以自己声明一个默认构造器吗? 不可以 可以 如果声明了自己的构造器,编译器还会生成默认构造器吗? 会 不会 如果在自己的构造器中不初始化一个字段,编译器会帮我们初始化吗? 不会 会 可以在声明一个实例字段的同时初始化它吗? 不可以 可以

structSample? aStructSample = null

public override string ToString(){    string data = String.Format("{0} {1} {2}",this,month,this.day,this.year);    return data;}

结构不能自定义复制构造函数,类没有复制构造函数,因为类是引用类型

int[] array1;array1 = new int[4];int size = int.Parse(Console.ReadLine());int[] array2 = new int[size];
int[] array = new int[3]{1,2,3,4};          //编译时错误int[] array = new int[4]{1,2,3};                //编译时错误int[] array = new int[4]{1,2,3,4};          //正确int[] array = {1,2,3,4};                    //正确var names = new[]{"John","Paul","James"}        //正确,其中names.Length的值为3var bad =new[]{"John","Paul",1993}          //正确,1993自动转换为字符串型  foreach (int i in array){    ...                       //此处不能通过i修改数组元素的值,i是副本}
int[] pins = {1,2,3,4};int[] copy =new int[pins.Length];pins.CopyTo(copy,0);
int[] pins = {1,2,3,4};int[] copy = new int[pins.Length];Array.Copy(pins,copy,copy.Length);
int[] pins = {1,2,3,4}int[] copy = (int[])pins.Clone();
int[,] items = new int[4,6];

抽象类和接口的区别
抽象类不能被实例化,而接口可以实例化来指向实现它的类(多态的体现)
所以在C++中 没有接口的设计 但是用抽象类代替了接口的功能

垃圾回收器的工作原理

垃圾回收器在它自己的线程中运行,而且只有在特定的时候才会执行(通常是应用程序抵达一个方法的结尾的时候)。它运行时,应用程序中运行的其它线程将暂停。这是由于垃圾回收器可能需要移动对象并更新对象引用;假如对象仍在引用,就不能进行这些操作。
垃圾回收器采取的步骤如下:

  1. 它构造包含所有可达对象的一个映射表map。为此,它会反复跟随对象中的引用字段。垃圾回收器会非常小心地构造这个map,并确保循环引用不会造成无限递归。任何不在这个map中的对象肯定是不可达的。
  2. 它检查是否有任何不可达的对象含有一个需要运行的析构器(运行析构器的过程称为finalization 或者说“终结”)。需要终结的任何不可达的对象都会放到一个特殊的队列中。这个队列称为freachable(发音是F-reachable)队列。
  3. 它回收剩余的不可达对象(即不需要终结的对象)。为此,它会在堆(heap)中向下移动可抵达的对象,从而对heap进行“碎片整理”,并释放位于heap顶部的内存。垃圾回收器移动了一个可达的对象后,还会更新该对象的任何引用。
  4. 然后,它允许其它线程恢复运行。
  5. 它在一个单独的操作中,对需要终结的不可达对象(现在,这些对象在freachable队列中了)执行finalize操作。

慎用析构器

索引器

索引可用于接口、类、结构体中

struct IntBits{    ...    public bool this [int index]    //整型index作为索引,返回bool型值    {        get        {            return (bits & (1 << index)) !=0;        }        set        {            if (value)                bits |= (1 << index);            else                bits &= ~(1 << index);        }    }    ...}
struct Wrapper{    private int[] data;    ...    public int this [int i]    //整型i作为索引,返回int型值    {        get { return this.data[i]; }        set { this.data[i] = value; }    }}

委托

class Controller{    delegate void stopMachineryDelegate();    private stopMachineryDelegate stopMachinery;    ...    public Controller()    {        this.stopMachinery += folder.StopFolding;    }    ...    public ShutDown()    {        this.stopMachinery();        ...    }}
Controller control = new Controller();FoldingMachine folder = new FoldingMachine();WeldingMachine welder = new WeldingMachine();PaintingMachine painter = new PaintingMachine();...control.Add(welder.FinishWelding);control.Add(painter.PaintOff);...control.ShutDown();...

适配器

void FinishFolding(){    folder.StopFolding(0);}

将lambda 表达式作为适配器使用

this.stopMachinery += (() => { folder.StopFolding(0); });
this.stopMachinery += (() => folder.StopFolding(0));
0 0