Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
AnswerRe: Playing media with 2 different channels in stereo file using NAudio library Pin
Eddy Vluggen10-Oct-14 7:53
professionalEddy Vluggen10-Oct-14 7:53 
QuestionWinform Change Label Text from secondary form action Pin
Derek Kennard9-Oct-14 1:56
professionalDerek Kennard9-Oct-14 1:56 
AnswerRe: Winform Change Label Text from secondary form action Pin
BillWoodruff9-Oct-14 3:00
professionalBillWoodruff9-Oct-14 3:00 
GeneralRe: Winform Change Label Text from secondary form action Pin
Derek Kennard9-Oct-14 15:28
professionalDerek Kennard9-Oct-14 15:28 
QuestionMake Cell Read Only after clicking the Cell in DataGridView Pin
avisharma@sharma9-Oct-14 1:28
avisharma@sharma9-Oct-14 1:28 
AnswerRe: Make Cell Read Only after clicking the Cell in DataGridView Pin
Simon_Whale9-Oct-14 1:36
Simon_Whale9-Oct-14 1:36 
GeneralRe: Make Cell Read Only after clicking the Cell in DataGridView Pin
avisharma@sharma9-Oct-14 2:05
avisharma@sharma9-Oct-14 2:05 
Questionusing "naked" Action/Func as EventHandlers ? C# language question PinPopular
BillWoodruff9-Oct-14 1:07
professionalBillWoodruff9-Oct-14 1:07 
I have been intrigued by the fact that you can use Func and Action instances as EventHandlers directly (after all, they are delegates). Using them "raw," without the usual business/ceremony of 'object sender, 'EventArgs e, does, of course, go "against the grain" of the conventional usage pattern for Events in C#.

Let me wallow in some mud code before I get to my question:

Using this very simple class:
C#
public class UseAGenericFunc<T>
{
    public Func<T, T, T> FuncToUse;
}

// there are a variety of ways you can use its internal Func:

// example instance declaration
private void TestUsingGenericFunc()
{
    UseAGenericFunc<string> testWithString = new UseAGenericFunc<string>();
    
    // direct assign of delegate instance (explicit Type declaration)
    testWithString.FuncToUse = delegate(string str1, string str2)
    {
        MessageBox.Show("direct delegate assignment");
        return str1 + str2;
    };
    
    // direct assign lambda (implicit Type inference by compiler)
    testWithString.FuncToUse = (str1, str2) =>
    {
        MessageBox.Show("lambda assignment");
        return str2 + str1;
    };

    // using an instance of a named Func
    Func<string, string, string> aStringFunc = delegate(string str1, string str2)
    {
        MessageBox.Show("use of named Func added by +=");
        return "named Func " + str1 + str2;
    };
    testWithString.FuncToUse += aStringFunc; 

    // add handler using +=
    testWithString.FuncToUse += delegate(string str1, string str2)
    {
        MessageBox.Show("delegate added by +=");
        return str1 + str2 + str1;
    };
    
    // add named Method as EventHandler using +=
    testWithString.FuncToUse += NamedMethodFuncToUse;

    // test
    string result = testWithString.FuncToUse("hello ", "goodbye");
}

// the named method
private string NamedMethodFuncToUse(string str1, string str2)
{
    MessageBox.Show("using named method");
    return "named method " + str1 + str2;
}
You are astute (well, at the least, competent) if you looked at the code above and anticipated that the test would result in four appearances of 'MessageBox. And, I am sure you understood that the second assignment (to a lambda expression) essentially discarded/replaced the first assignment (to a "formal" delegate).

Of course, if you assign an anonymous method as an EventHandler, you are never going to be able to remove it from the delegate's method dispatch queue using the -= operator. So, that's one "big" argument in favor of assigning named methods, or, in this case, named instances of Func.

Finally (sound of large gong), the question(s):

Do you make use of "raw" Action or Func delegates in your coding practice; if so, why, and in what contexts do you find them indicated, or useful ?

If you think such usage of Action and Func are very bad things, why ?

thanks, Bill
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali

AnswerRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen9-Oct-14 9:04
professionalEddy Vluggen9-Oct-14 9:04 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff9-Oct-14 11:13
professionalBillWoodruff9-Oct-14 11:13 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen10-Oct-14 7:38
professionalEddy Vluggen10-Oct-14 7:38 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Richard Deeming9-Oct-14 22:20
mveRichard Deeming9-Oct-14 22:20 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen10-Oct-14 7:50
professionalEddy Vluggen10-Oct-14 7:50 
AnswerRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty10-Oct-14 5:24
professionalNicholas Marty10-Oct-14 5:24 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff10-Oct-14 11:23
professionalBillWoodruff10-Oct-14 11:23 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty13-Oct-14 1:13
professionalNicholas Marty13-Oct-14 1:13 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff13-Oct-14 1:34
professionalBillWoodruff13-Oct-14 1:34 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty13-Oct-14 2:17
professionalNicholas Marty13-Oct-14 2:17 
QuestionTransaction in active directory Pin
Nitin K8-Oct-14 23:21
Nitin K8-Oct-14 23:21 
AnswerRe: Transaction in active directory Pin
Eddy Vluggen9-Oct-14 0:30
professionalEddy Vluggen9-Oct-14 0:30 
QuestionHow to make custom pagination when use grid view in c# Pin
heyvid8-Oct-14 21:10
heyvid8-Oct-14 21:10 
AnswerRe: How to make custom pagination when use grid view in c# Pin
Richard MacCutchan8-Oct-14 21:42
mveRichard MacCutchan8-Oct-14 21:42 
AnswerRe: How to make custom pagination when use grid view in c# Pin
Maciej Los9-Oct-14 10:56
mveMaciej Los9-Oct-14 10:56 
GeneralHow to Create task bar in c# windows app Pin
Srikanth598-Oct-14 3:51
Srikanth598-Oct-14 3:51 
GeneralRe: How to Create task bar in c# windows app Pin
DaveAuld8-Oct-14 3:58
professionalDaveAuld8-Oct-14 3:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.