两种类型的多维度数组
矩形数组(rectangular array),交错数组(jagged array)。


矩形数组:不管有多少维度,总是使用一个组方括号。

int x=myArray[4, 6];

 

交错数组:每个子数组都是独立数组的多维度数组,可以有不同长度的子数组。
int  x = jaggedArray[2][7][4];


数组是引用对象,引用在栈或堆上,而数组对象本身总是在堆上。

值类型数组和引用类型数组:

 委托:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;

namespace ConsoleApplication1
{
    delegate void MyDel( int i);

    class A
    {
        public void print1( int i)
        {
            i += 5;
            Console.WriteLine(i);
        }

        public static void print2( int i)
        {
            i += 6;
            Console.WriteLine(i);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int k = 2;
            MyDel md = null;
            A a = new A();

            md += new MyDel(a.print1);
            md += A.print2;

            md += (int j) => 
            {
                j += 2;
                Console.WriteLine(j);
                k++;
            };

            md(k);

            Console.WriteLine(k);

            Console.ReadKey();
        }
    }
}
View Code

Note: Lambda表达式的各种简写方式。直接将方法(实例方法或静态方法)赋值给委托对象,是一种简写,其中存在隐式转换。

 

事件:
事件的很多部分和委托类似。实际上,事件就像是专门用于某种特殊用途的简单委托。事件包含了一个私有的委托,如下图。

有关事件的私有委托需要了解的重要事项
1. 事件提供了对它私有控制委托的结构化访问,也就是说,你无法直接访问委托。
2. 事件中可用的操作比委托要少,对于事件我们只可以添加,删除或调用事件处理程序。
3. 事件被触发时,它调用委托来依次调用列表中的方法。
4. 除了调用事件本身外,+=和-=运算符是事件唯一允许的操作。

 例如: 在类ClassA中定义两个一个事件,public event  EventHandler CountedADozen;   事件CountedADozen只有在类内部可以: CountedADozen(); 在类外部,只能在+=或-=的左边。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;

namespace ConsoleApplication1
{
    class Incrementer
    {
        public event  EventHandler CountedADozen;

        public void DoCount()
        {
            for (int i = 0; i<10; i++)
            {
                 if(i % 4 == 0)
                 {
                     if (CountedADozen != null)
                     {
                         CountedADozen(this, null);
                     }
                 }
            }
        }
    }

    class Dozens
    {
        public int DozensCount { get; private set; }

        public Dozens(Incrementer incrementer)
        {
            DozensCount = 0;
            incrementer.CountedADozen += new EventHandler(IncrementerDozensCount);
            incrementer.CountedADozen += new EventHandler(IncrementerDozensCount);

            //incrementer.CountedADozen -= new EventHandler(IncrementerDozensCount1);
        }

        void IncrementerDozensCount(object sender, EventArgs e)
        {
            DozensCount++;
        }

        void IncrementerDozensCount1(object sender, EventArgs e)
        {
            DozensCount++;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Incrementer incrementer = new Incrementer();
            Dozens dozens = new Dozens(incrementer);


            incrementer.CountedADozen(null, null);   //cannot compile

            incrementer.DoCount();

            Console.WriteLine(dozens.DozensCount); 

            Console.ReadKey();
        }
    }
}
View Code

 

事件访问器:

 

posted on 2018-06-07 15:34  zzunstu  阅读(569)  评论(0编辑  收藏  举报