C# 代表的实际执行
在我看来,代表似乎是一个需要学习的具有挑战性的概念 根据我的理解,委托是一个方法指针,它在运行时指向一个特定的方法 我为代表举的一个例子是在文件处理期间,可以在调用方法之前执行一些文件操作,并在方法调用之后释放文件资源。在这里使用委托可以提高重用性 我的问题是,你能告诉我日常编程中代理的其他实际用途吗C# 代表的实际执行,c#,C#,在我看来,代表似乎是一个需要学习的具有挑战性的概念 根据我的理解,委托是一个方法指针,它在运行时指向一个特定的方法 我为代表举的一个例子是在文件处理期间,可以在调用方法之前执行一些文件操作,并在方法调用之后释放文件资源。在这里使用委托可以提高重用性 我的问题是,你能告诉我日常编程中代理的其他实际用途吗 感谢是前进 总的来说,委托最主要的用途是通过事件及其处理程序。我不知道你是否意识到这一点,因为你的问题措辞的方式,但每次你写 someObj.SomeEvent += SomeMethod; 您
感谢是前进 总的来说,委托最主要的用途是通过事件及其处理程序。我不知道你是否意识到这一点,因为你的问题措辞的方式,但每次你写
someObj.SomeEvent += SomeMethod;
您正在使用委托,特别是,
SomeMethod
被委托实例包装。总体而言,委托最主要的用途是通过事件及其处理程序。我不知道你是否意识到这一点,因为你的问题措辞的方式,但每次你写
someObj.SomeEvent += SomeMethod;
您正在使用委托,特别是,SomeMethod
被委托实例包装。签出
在这里输入太多了请检查
在这里输入太多了您还将使用带线程的委托:
//Anynymous delegate
new Thread(delegate()
{
Console.WriteLine("Hello world form Thread");
});
//Lambda expression
new Thread(() =>
{
Console.WriteLine("Hello world form Thread");
});
看看lambda表达式,它很强大:您还将在线程中使用委托:
//Anynymous delegate
new Thread(delegate()
{
Console.WriteLine("Hello world form Thread");
});
//Lambda expression
new Thread(() =>
{
Console.WriteLine("Hello world form Thread");
});
看看lambda表达式,它很强大:委托在与自定义事件处理程序一起使用时很有用 委托是可以在以下位置为调用方法定义的规则: 运行时 例如,公共委托无效名称指示器(字符串名称) 可以将方法绑定到委托并将其注册到事件 请参见下面的示例
public delegate void NameIndicator( string name );
class Program
{
static void Main( string[] args )
{
//Create the instance of the class
Car car = new Car( "Audi" );
//Register the event with the corresponding method using the delegate
car.Name += new NameIndicator( Name );
//Call the start to invoke the Name method below at runtime.
car.Start();
Console.Read();
}
/// <summary>
/// The method that can subscribe the event of the defined class.
/// </summary>
/// <param name="name">Name assigned from the caller.</param>
private static void Name( string name )
{
Console.WriteLine( name );
}
}
public class Car
{
//Event for the car class.
public event NameIndicator Name;
string name;
public Car( string nameParam )
{
name = nameParam;
}
//Invoke the event when the start method is called.
public virtual void Start()
{
Name( name );
}
}
公共委托无效名称指示器(字符串名称);
班级计划
{
静态void Main(字符串[]参数)
{
//创建类的实例
轿车=新车(“奥迪”);
//使用委托使用相应的方法注册事件
车辆名称+=新名称指示器(名称);
//调用start在运行时调用下面的Name方法。
汽车启动;
Console.Read();
}
///
///可以订阅已定义类的事件的方法。
///
///从调用方分配的名称。
私有静态无效名称(字符串名称)
{
Console.WriteLine(名称);
}
}
公车
{
//汽车类的活动。
公共事件名称指示器名称;
字符串名;
公共汽车(字符串名称参数)
{
name=nameParam;
}
//调用start方法时调用事件。
公共虚拟void Start()
{
姓名(姓名);
}
}
委托与自定义事件处理程序一起使用时非常有用
委托是可以在以下位置为调用方法定义的规则:
运行时
例如,公共委托无效名称指示器(字符串名称)
可以将方法绑定到委托并将其注册到事件
请参见下面的示例
public delegate void NameIndicator( string name );
class Program
{
static void Main( string[] args )
{
//Create the instance of the class
Car car = new Car( "Audi" );
//Register the event with the corresponding method using the delegate
car.Name += new NameIndicator( Name );
//Call the start to invoke the Name method below at runtime.
car.Start();
Console.Read();
}
/// <summary>
/// The method that can subscribe the event of the defined class.
/// </summary>
/// <param name="name">Name assigned from the caller.</param>
private static void Name( string name )
{
Console.WriteLine( name );
}
}
public class Car
{
//Event for the car class.
public event NameIndicator Name;
string name;
public Car( string nameParam )
{
name = nameParam;
}
//Invoke the event when the start method is called.
public virtual void Start()
{
Name( name );
}
}
公共委托无效名称指示器(字符串名称);
班级计划
{
静态void Main(字符串[]参数)
{
//创建类的实例
轿车=新车(“奥迪”);
//使用委托使用相应的方法注册事件
车辆名称+=新名称指示器(名称);
//调用start在运行时调用下面的Name方法。
汽车启动;
Console.Read();
}
///
///可以订阅已定义类的事件的方法。
///
///从调用方分配的名称。
私有静态无效名称(字符串名称)
{
Console.WriteLine(名称);
}
}
公车
{
//汽车类的活动。
公共事件名称指示器名称;
字符串名;
公共汽车(字符串名称参数)
{
name=nameParam;
}
//调用start方法时调用事件。
公共虚拟void Start()
{
姓名(姓名);
}
}
谢谢您,先生。这是非常有益的。我期待着您的帖子。谢谢先生。这是非常有益的。我期待着你的帖子。感谢你对RajKumar的支持。感谢你对RajKumar的支持。感谢你,我对与代表一起使用线程的意识有所提高,因为我不知道这一点。我期待着你的帖子。多亏了你,我对在代理中使用线程的意识有所提高,因为我没有意识到这一点。我期待着你的职位。