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

C#

 
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 
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 
I usually use the EventHandler (or EventHandler<T> if I have any arguments to pass) type for events. This has a few advantages for me:
1. I follow a general pattern and know what to expect when I see it. I know I can subscribe and unsubscribe the events.
2. When Unit-Testing classes that handle events from other classes I can easily fake them using a mocking framework (NSubstitute is my preferred method) which already provides a very easy way to raise a certain event so I can test the behavior of my class.

Examples for firing events with NSubstitute: Link[^]
If the Event is of the type EventHandler I don't even have to specify any arguments as the framework passes the sender and EventArgs.Empty per default for me.
3. The IDE (maybe because of Resharper) helps me with creating the event invocators so I don't have to do that manually (or invoke the event without subscribers which might cause errors)

I do use Action and Func for other things though.
I regularly use(d) the Action when I code asynchronous things. I can just pass a method as a callback method (It's similiar to Microsofts pattern with the AsyncCallback type which is just a more specialized version of an Action). And I used Func for things like accessing an in memory list with Linq with different filters. (e.g. You could load the data from the cache etc. and reload from a data source if the cache has expired, etc.)

Example:
C#
private List<MyObject> MyList;
void Main()
{
   var items1 = Get(x => x.MyInt == 1);
   var items2 = Get(x => x.MyBool == false);
   var items3 = Get(x => x.MyString == "XXX");
   var items4 = Get(x => x.MyBool == false && x.MyString == "XXX");
}

public IEnumerable<MyObject> Get(Func<MyObject, bool> filter) {
	if(MyList == null) MyList = new List<MyObject>();
	if(MyList.Count==0) {
		MyList.Add(new MyObject { MyInt = 1, MyBool = true, MyString = "XXX" });
		MyList.Add(new MyObject { MyInt = 2, MyBool = false, MyString = "XXX" });
		MyList.Add(new MyObject { MyInt = 3, MyBool = false, MyString = "YYY" });
	}
	return MyList.Where(filter);
}

// a data object contained in the list
public class MyObject {
   public int MyInt { get; set; }
   public bool MyBool { get; set; }
   public string MyString { get; set; }
}


This way I can handle the behaviour of accessing and loading the data in one central location.

So I usually use the Action and Func when I pass them as parameters to a method and the classic EventHandler for events.
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 
JokeRe: How to Create task bar in c# windows app Pin
Duncan Edwards Jones8-Oct-14 4:04
professionalDuncan Edwards Jones8-Oct-14 4:04 
GeneralRe: How to Create task bar in c# windows app Pin
enhzflep8-Oct-14 3:59
enhzflep8-Oct-14 3:59 
GeneralRe: How to Create task bar in c# windows app Pin
Richard MacCutchan8-Oct-14 4:19
mveRichard MacCutchan8-Oct-14 4:19 
QuestionC# Window form application not working another computer Pin
coolsuhail8-Oct-14 3:39
coolsuhail8-Oct-14 3:39 
AnswerRe: C# Window form application not working another computer Pin
Dominic Burford8-Oct-14 4:00
professionalDominic Burford8-Oct-14 4:00 
GeneralRe: C# Window form application not working another computer Pin
coolsuhail10-Oct-14 1:47
coolsuhail10-Oct-14 1:47 

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.