正确使用代表。 [英] Proper use of delegates.

查看:59
本文介绍了正确使用代表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我们目前正处于不同的

线程中的多线程主题,我对代表有疑问。


当我使用代表时,我只为每个不同的参数集创建一个
我需要的



例如:


委托void callBackString(string d_string);


委托void callBackStringArray(string [] d_string);


委托void callBackInt(int d_int);


委托void callBackSB(字符串d_string,bool d_bool);


....然后我只是在我需要的时间和地点重复使用它们。


这是使用代表的正确方法吗?


如果不是这样,我对最有效的方式非常感兴趣它。


谢谢


-

Roger Frost

"逻辑是语法独立的

解决方案

泛型 - 但它已经为你完成了:c onsider Action< T>?

即Action< string>,Action< string []和Action< intetc匹配您的前3

示例


..NET 3.5引入了多参数变体:

Action< string,boolmatches你的最后一个例子


如果委托应返回一个值,然后Func< ...也可用 -

ie

" int Foo(string arg)"匹配Func< string,int>


Marc


" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言

新闻:MP ********************* @ msnews.microsoft.com 。 ..


Roger Frost< fr ***** @ hotmail.comwrote:


> ;>
当我使用委托时,我只为每个不同的参数集创建一个
我需要的。

例如:

委托无效callBackString(string d_string);

...然后我只是在我需要的时间和地点重用它们。

这是使用委托的正确方法吗?
如果不是这样,我对最有效的方式非常感兴趣。



1)你要声明一个类型,所以使用类型的常规约定

名称:Pascal case。



约定是我努力的事情,希望能够变得更好。

当我只是在编写程序时,它就是不是问题,但我知道我需要学习标准......我很欣赏我的错误指出

out。


>

2)如果你使用的是.NET 3.5,你可以使用内置的Action< ...>

代表;您的上述代表相当于


操作<字符串>

操作< string []>

操作< int>

操作< string,bool>



Yup .NET 3.5,我将优化我的代码。


谢谢!


2008年2月21日星期四23:22:22 -0800,Roger Frost< fr ***** @ hotmail.com>

写道:


由于我们目前正处于多线程的主题中,因此我们将讨论另一个问题。 $ b线程,我对代表有疑问。


当我使用委托时,我只需为每个不同的参数集创建一个
我需要的
。 br />

[...]

...然后我只需要在需要的时间和地点重复使用它们。


这是使用代表的正确方法吗?



好​​吧,对于返回void的单参数委托,你可以使用

通用Action< Tdelegate。因此,举例来说,你要声明并使用委托void callBackString(string d_string),你会跳过

声明,只是使用Action< string>相反,你将使用

使用callBackString作为类型。还有一个无参数代表

名为Action。显然是非通用的。


如果您使用的是.NET 3.5,他们已经添加了两个,三个和四个参数

版本相同的通用类型。如果您需要四个以上的参数,那么您可以考虑为每个

参数计数声明一个通用委托类型,根据需要重新使用,而不是制作一个

每个超过四个参数的新动作。


例如:


委托无效动作< T1,T2,T3,T4,T5>(T1 t1,T2 t2,T3 t3,T4

t4,T5 t5);


当然,如果你有一个动作的参数超过四个,你可能会有其他问题。但至少你可以避免让所有那些代表

类型。 :)


如果您没有使用.NET 3.5,那么您当然可以声明两个,

三个和四个参数动作委托类型自己,就像上面的

五参数示例一样(当然,参数更少:))。


Pete


Since we are currently on the subject of multi-threading in a different
thread, I have a question about delegates.

When I use delegates, I just create one for each different parameter set
that I need.

For example:

delegate void callBackString(string d_string);

delegate void callBackStringArray(string[] d_string);

delegate void callBackInt(int d_int);

delegate void callBackSB(string d_string, bool d_bool);

....And then I just reuse them when and where I need them.

Is this the proper way to use delegates?

I''m highly interested in the most efficient way if this isn''t it.

Thanks

--
Roger Frost
"Logic Is Syntax Independent"

解决方案

Generics - but it is already done for you: consider Action<T>?
i.e. Action<string>, Action<string[]and Action<intetc match your first 3
examples

..NET 3.5 introduces multi-paramater variants:
Action<string,boolmatches your last example

If the delegate should return a value, then Func<...is also available -
i.e.
"int Foo(string arg)" would match Func<string,int>

Marc


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..

Roger Frost <fr*****@hotmail.comwrote:

>>
When I use delegates, I just create one for each different parameter set
that I need.

For example:

delegate void callBackString(string d_string);
...And then I just reuse them when and where I need them.

Is this the proper way to use delegates?

I''m highly interested in the most efficient way if this isn''t it.


1) You''re declaring a type, so use the normal convention for type
names: Pascal case.

Conventions are something that I struggle with and hope to get better at.
When it is just me writing programs, it''s not a problem, but I know that I
need to learn the standards...and I appreciate having my mistakes pointed
out.

>
2) If you''re using .NET 3.5 you can use the built-in Action<...>
delegates; your delegates above are equivalent to

Action<string>
Action<string[]>
Action<int>
Action<string,bool>

Yup .NET 3.5, I will optimize my code for this.

Thanks!


On Thu, 21 Feb 2008 23:22:22 -0800, Roger Frost <fr*****@hotmail.com>
wrote:

Since we are currently on the subject of multi-threading in a different
thread, I have a question about delegates.

When I use delegates, I just create one for each different parameter set
that I need.

[...]
...And then I just reuse them when and where I need them.

Is this the proper way to use delegates?

Well, for single-parameter delegates that return void, you could just use
the generic Action<Tdelegate. So, for example, where you''d declare and
use "delegate void callBackString(string d_string)", you''d skip the
declaration and just use "Action<string>" instead where you would have
used "callBackString" as the type. There''s also a no-parameter delegate
named Action. Non-generic, obviously.

If you''re using .NET 3.5, they''ve added two, three, and four-parameter
versions of the same generic type. If you need more than four parameters,
then you might consider declaring a single generic delegate type for each
parameter count you need that you reuse as necessary, rather than making a
new one for each more-than-four-parameter action you have.

For example:

delegate void Action<T1, T2, T3, T4, T5>(T1 t1, T2 t2, T3 t3, T4
t4, T5 t5);

Of course, if you''ve got more than four parameters for an action, you may
have other issues. But at least you can avoid making all those delegate
types. :)

If you''re not using .NET 3.5, then you could of course declare the two-,
three-, and four-parameter Action delegate types yourself, as with the
five-parameter example above (except with fewer parameters, of course :) ).

Pete


这篇关于正确使用代表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆