C# 基础学习笔记

sealed 非抽象,不可被继承(同final) 和abstract相对应


explicit 显示的


implicit 隐式




const 静态常量 Compile-time constant  不能用static修饰
readonly 动态常量 Runtime constant 可以用static修饰


ref 引用
out 形式参数 也是引用传递方式
params 引用 一维 最后 出现一次




virtual 和 override 和 new 的使用


is 和 as 检查类型转换是否可行  不可行as返回的是null,is返回的是false


代理和事件

// 代理和事件
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
    // 自定义事件ClockTimerArgs类,继承自EventArgs
    public class ClockTimerArgs : EventArgs
    {
        private int tickCount;
        public ClockTimerArgs(int tickCount)
        {
            this.tickCount = tickCount;
        }
        public int TickCount
        {
            get
            {
                return tickCount;
            }
        }
    }
    // 定义事件的代理
    public delegate void TimerEvent(object sender,ClockTimerArgs e);
    // 事件触发类
    class ClockTimer
    {
        public event TimerEvent Timer;
        public void Start()
        {
            for (int i = 0; i < 5; i++)
            {
                // 触发事件
                Timer(this, new ClockTimerArgs(i+1));
                Thread.Sleep(1000);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ClockTimer clockTimer = new ClockTimer();
            clockTimer.Timer += new TimerEvent(onClockTick);
            clockTimer.Start();
        }
        // 事件处理函数
        public static void onClockTick(object sender,ClockTimerArgs e)
        {
            Console.WriteLine("收到时钟事件{0}!!!",e.TickCount);
        }
    }
}


输出
收到时钟事件1!!!
收到时钟事件2!!!
收到时钟事件3!!!
收到时钟事件4!!!
收到时钟事件5!!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值