C#中所有的关键字以及委托例

来源:互联网 发布:c语言从偶数2加到100 编辑:程序博客网 时间:2024/06/14 08:43
C#所有关键字abstractbaseboolbreakbytecasecatchcharcheckedclassconstcontinuedecimaldefaultdelegatedodoubleelseenumevent explicitexternfalsefinallyfixedfloatforforeachgotoifimplicitinintinterfaceinternalislocklongnamespacenewnullobjectoperatoroutoverrideparamsprivateprotectedpublicreadonlyrefreturnsbytesealedshortsizeofstaticstringstructswitchthisthrowtruetrytypeofuintulonguncheckedunsafeushortusingvirtualvoidwhile 

类:public 访问不受限制

       internal 只在程序集内部可访问

方法函数: public 不受限制

                  internal 只在程序集内部可访问

                  protected 访问仅限于包含类或者从包含类派生的子类中

                  protected internal 访问仅限于类及从类派生的子类中,或者当前程序集的其他类

                 private   访问仅限于包含的类

 

委托:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    /// <summary>
    /// 定义一个委托(委托类的对象就是事件)
    /// </summary>
    delegate void Event1Handler(object sender,EventArgs e);
   /// <summary>
   /// 定义一个Button类,有一个Click事件(创建事件发布者类)
   /// </summary>
    class Button
    {

        public event Event1Handler Event1;
        public void Click()
        {
            if (Event1 != null)
            {
                Event1(null, null);
            }
        }
    }
    /// <summary>
    /// 定义一个Form类(创建事件的接收者类)
    /// </summary>
    class Form
    {
        public Form(Button btn)
        {
            btn.Event1 += new Event1Handler(btn_Event1);
        }
        void btn_Event1(object sender,EventArgs e)
        {
            Console.WriteLine("启动C# 事件");
            Console.ReadLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Button btn = new Button();//new 一个button对象
            Form form = new Form(btn);//new 一个Form对象
            btn.Click();
        }
    }
}

 Checked 以及unChecked语句被用来控制数字运算的溢出检查;

原创粉丝点击