c#


Is List<T> where T is an anonymous delegate possible?


Is it possible to create a list of anonymous delegates in C#? Here is code I would love to write but it doesn't compile:
Action<int> method;
List<method> operations = new List<method>();
You can write this, for example
Action<int> method = j => j++;
List<Action<int>> operations = new List<Action<int>>();
operations.Add(method);
operations.Add(i => i++);
The problem with your code is that you are trying to specify an instance as a type argument to List.
Action<int> method is an instance, whereas Action<int> is a type.
As another poster mentions, you just need to declare the type of list as Action<int>, that is, with just the type parameter.
e.g.
var myNum = 5;
var myops = new List<Action<int>>();
myops.Add(j => j++);
myops.Add(j => j++);
foreach(var method in myops)
{
Console.WriteLine(method(myNum));
}
// Frowned upon, but fun syntax
myops.Each(method => method(myNum));
It's certainly possible to create a list of a specific delegate type, like Action or Func, or any others. Since anonymous delegates can be cast to any delegate type with a compatible signature, you can create a list of delegates as long as they have compatible signatures.
I don't think creating a list of delegates with multiple kinds of signatures would be of much use, since having an unspecified signature would make it very hard to call a delegate. You should be able to do so with reflection, though. In that case you can just use a list of object.

Related Links

Taskpool system for .NET
C# Remove padding from TabPages in Windows Forms
Serialization .txt vs .xml
Getting an image from an RSS feed, using syndication?
Modifying or replacing column values reading from the DataTable
Best Practices for Lookup Tables in EF Code-First
Reading cookie in c#
rounding decimal to special number of digits
Debugging assembly load issue in C#
The process cannot access the file '' because it is being used by another process
Subscribing to COM events in managed COM assembly from C#
DataSet sorting
MonoTouch ResignFirstResponder without knowing which UIView is it
How use multitype Model in one layout (with partial)?
Incorrect syntax when pass parameter
Multiple sessions are created in oracle db by dotnet web application in visual studio 2008 in live server

Categories

HOME
http
kendo-ui-angular2
compression
locale
cxf
okhttp3
sugarcrm
swap
rtc
phpexcel
minimax
cakephp-2.0
yui
web-worker
dompdf
k-means
filehelpers
windows-phone
indri
datasource
google-drive-android-api
ps
scalatest
expressionengine
hierarchical-data
sqldependency
ipython-parallel
ios9
apm
scene7
epub
gstreamer-1.0
linkurious
mediastream
blueimp
mtm
amazon-rds-aurora
codepen
skype-bots
perldoc
photoshop-script
sharpssh
seyren
hellosign
scrollmagic
catalyst
recursive-query
vertex-shader
blackberry-webworks
disconnect
hotkeys
mrunit
angular-i18n
salesforce-service-cloud
video-embedding
matlab-engine
lytro
jain-sip
eaaccessory
playn
web-standards
android-networking
avd
sicstus-prolog
skview
service-broker
douglas-peucker
qxorm
caption
report-builder2.0
system.io.packaging
react-os
gnip
nsnotificationcenter
step
rda
iwork
fade
azure-scheduler
spawn
microsoft-speech-platform
django-tinymce
qt5.1
m3u
winrt-async
systrace
pdf-reader
bash4
animationdrawable
peoplepicker
funambol
jexcelapi
pkcs#7
morfik
file-storage
jgrowl
high-load

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App