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

Jenkins unable to build C#6 features until second execution
How to Resize the polygon by programmatically using ScaleTransform?
C# ASP.NET project with Facebook login - not working
ASP.NET MVC Attribute Routing - parameter is always null
How to create an Image Thumbnail in Unity3D on Android through C# code?
Postgres 9.6 transaction does not complete where expected with Entity Framework Core
Angular 2 MVC 5 Razor - How does one create an #Html.DropDownFor with angular attributes?
Merging XML Config File w/ .exe
Using LINQ to return a list of Ages by category
C# - How to create a KeyValuePair with the result of two foreach loops?
Re-writing HttpContext.Request.AnonymousID in mvc action
Simple middleware example. Invoke method dosn't continue processing
Office OpenXml How to read the content of a CustomPart
Display a text box only in Windows 10 (desktop)
Export varbinary to JSON File
iQueryable LINQ lambda OrderByDescending() is slower than OrderBy()

Categories

HOME
skype-for-business
kendo-ui
virtual-machine
locale
bigtable
x86-64
itext7
opacity
symfony-3.2
rethinkdb
google-api-oauth
dcos
point-of-sale
swift3.1
sha1
infopath
template10
indri
google-street-view
legend-properties
summernote
computation-theory
gomobile
git-branch
typedef
expressionengine
mongoose-im
hierarchy
folder
edirectory
schedule
intermec
akamai
ibm-bpm
postgresql-9.6
scene7
leaderboard
android-databinding
scip
form-fields
origami
usernotifications
office-interop
socketcluster
colorbox
togglebutton
flask-socketio
ajv
liferay-ide
try-catch-finally
sharing
angularfire
django-taggit
periodic-processing
wizard
wordpress-plugin
jquery-draggable
app-data
windows-phone-8-emulator
cyberduck
winforms-interop
grails-3.0.9
tinymce-plugins
sicstus-prolog
maven-archetype
register-allocation
selenium-grid2
atg-dynamo
rda
html5-filesystem
carrier
node-amqp
thucydides
friendly-id
fade
nsnumber
friendly-url
mosix
ntdll
wpml
github-archive
copyfile
lightopenid
service-layer
zipstream
javah
s60
for-xml
webdb
versions
focus-stealing
atmega16
scalable

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