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

Golang - DTOs, Entities and Mapping
New number to words
How do I set a role for first account created in ASP.NET MVC?
Adding Items From Database to ListView (Columns) - Visual Studio (C#)
Comparing 2 text files, and exporting the lines that don't exists in file no.1
How to allocate compound objects from a pool in Google.Protobuf C#
OpenGL rotating a 3d cylinder
ASP.Net: TabContainer: TabPanel Click Event
Is there a way to make a snippet apply itself to each line in a selection independently?
Trying to check if the user has input the correct text c#
How to add an image to the pictureplace holder in c# using Microsoft.Office.Interop.Word?
MvvmCross; How to RaisePropertyChange from another ViewModel
Post data in C# and use response in CefSharp
Communication between Web Services failed before sooner than the Timeout
Custom attribute handling in Json.Net
Invalid source control bindings after moving code

Categories

HOME
joomla
pyspark
packet
xamarin.forms
gap-system
skypedeveloper
overrides
download
richtextbox
schema
kairosdb
mbed
browsermob
element
saucelabs
hql
popup
nsoperation
openscad
internet-explorer-7
vivado-hls
windows-phone
android-actionbar
connectiq
pushbullet
advertising
datastax-enterprise
jira-zephyr
sasl
knapsack-problem
tftp
boosting
angular-schema-form
concrete5
deferred
exacttarget
loopback
directions
ilog
offset
var
stdin
fisheye
mixed-models
wicket-tester
aura-framework
jquery-ui-slider
vpython
text-extraction
togglebutton
text-formatting
geminabox
stream-socket-client
import.io
lucee
extjs3
audio-fingerprinting
r-commander
selection-sort
trusted-computing
cardview
qt3d
huawei
konakart
mate-desktop
netlink
falcon
jmap
stdlist
markerspiderfier
gtkwave
jcomponent
php-gd
notice
log4cxx
dpkt
litedb
euro
chrome-mobile
qxorm
launcher
jsonmodel
clearcanvas
powershell-v1.0
oxygenxml
amzi-prolog
email-headers
fastcgi-mono-server
internet-radio
mixradio
facebook-likebox
scidb
mandelbrot
wpd
infinity.js
system.transactions
gpl
rfc822
ios6-maps
filedialog
automapper-2
cherokee
execjs
youtube.net-api
mass-emails
mediastreamsource
chromebug
calendarextender
focus-stealing
pixel-bender
html-help-workshop
qlibrary

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