sealed 非抽象,不可被继承(同final) 和abstract相对应
explicit 显示的
implicit 隐式
const 静态常量 Compile-time constant 不能用static修饰
readonly 动态常量 Runtime constant 可以用static修饰
ref 引用
out 形式参数 也是引用传递方式
params 引用 一维 最后 出现一次
virtual 和 override 和 new 的使用
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!!!