我有一个场景,我想使用方法组语法而不是匿名方法(或lambda语法)来调用函数.
该函数有两个重载,一个需要一个Action,另一个需要一个Func<string>.
我可以愉快地使用匿名方法(或lambda语法)调用两个重载,但如果我使用方法组语法,则会获得Ambiguous调用的编译器错误.我可以明确的解决方法铸造到Action或Func<string>,但不认为这应该是必要的.
任何人都可以解释为什么应该要求显式演员表.
代码示例如下.
class Program
{
static void Main(string[] args)
{
ClassWithSimpleMethods classWithSimpleMethods = new ClassWithSimpleMethods();
ClassWithDelegateMethods classWithDelegateMethods = new ClassWithDelegateMethods();
// These both compile (lambda syntax)
classWithDelegateMethods.Method(() => classWithSimpleMethods.GetString());
classWithDelegateMethods.Method(() => classWithSimpleMethods.DoNothing());
// These also compile (method group with explicit cast)
classWithDelegateMethods.Method((Func<string>)classWithSimpleMethods.GetString);
classWithDelegateMethods.Method((Action)classWithSimpleMethods.DoNothing);
// These both error with "Ambiguous invocation" (method group)
classWithDelegateMethods.Method(classWithSimpleMethods.GetString);
classWithDelegateMethods.Method(classWithSimpleMethods.DoNothing);
}
}
class ClassWithDelegateMethods
{
public void Method(Func<string> func) { /* do something …Run Code Online (Sandbox Code Playgroud) 这个问题分为两部分:
是否提高事件阻塞线程,或者它异步启动事件处理器的执行和线程便在同一时间继续吗?
是单独的事件处理器(订阅事件)运行同步一个接一个,或者是他们还不能保证别人不会在同一时间运行的异步运行?
我正在写这样的代码,做一些快速和肮脏的时间:
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
b = DoStuff(s);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Run Code Online (Sandbox Code Playgroud)
肯定有一个方法来调用此位的计时码作为花式schmancy .NET 3.0的λ,而不是(上帝保佑)剪切和粘贴了几次和更换DoStuff(s)用DoSomethingElse(s)?
我知道它可以作为一个Delegate但我想知道lambda方式.
重复:如何确保事件仅订阅一次 并且已添加事件处理程序?
我有一个提供一些服务的单例,我的类挂钩到它上面的一些事件,有时一个类挂起两次事件然后被调用两次.我正在寻找一种经典的方法来防止这种情况发生.不知怎的,我需要检查一下我是否已经迷上了这个事件......
每当完全加载UICollectionView时,我必须做一些操作,即那时应该调用所有UICollectionView的数据源/布局方法.我怎么知道?是否有任何委托方法来了解UICollectionView加载状态?
任何人都可以解释这个链接上写的这个声明
Invoke(Delegate):
Run Code Online (Sandbox Code Playgroud)
在拥有控件的基础窗口句柄的线程上执行指定的委托.
任何人都可以解释这意味着什么(尤其是大胆的)我无法清楚地理解它
这有什么区别:
@property (nonatomic, weak) id <SubClassDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)
还有这个:
@property (nonatomic, assign) id <SubClassDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)
我想为代表使用属性.
我已经看到了一些关于这个习语的提及(包括SO):
// Deliberately empty subscriber
public event EventHandler AskQuestion = delegate {};
Run Code Online (Sandbox Code Playgroud)
好处很明显 - 它避免了在提升事件之前检查null的必要性.
但是,我很想知道是否有任何缺点. 例如,它是否被广泛使用并且足够透明以至于不会引起维护问题?空事件用户呼叫是否有明显的性能影响?
我需要在班上有一些代表.
我想使用界面"提醒"我设置这些代表.
如何?
我的班级看起来像这样:
public class ClsPictures : myInterface
{
// Implementing the IProcess interface
public event UpdateStatusEventHandler UpdateStatusText;
public delegate void UpdateStatusEventHandler(string Status);
public event StartedEventHandler Started;
public delegate void StartedEventHandler();
}
Run Code Online (Sandbox Code Playgroud)
我需要一个界面来强制这些代表:
public interface myInterface
{
// ?????
}
Run Code Online (Sandbox Code Playgroud) delegates ×10
c# ×8
.net ×3
events ×2
coding-style ×1
idioms ×1
interface ×1
invoke ×1
ios ×1
ios5 ×1
lambda ×1
objective-c ×1
properties ×1
reload ×1
winforms ×1