Question

It's 2013, and a major platform coughUnity3dcough is seemingly still partly stuck on 2.x (latest Unity is newer, but isn't production-quality yet for some features). It seems there's a lot of stuff that was missing from the language until 3.5 - e.g. Func and Action - so I'm trying to find a way to do this without using 3.x.

Here's a great, very neat solution for 3.x: Pass Method as Parameter using C#

I tried Microsoft's site, but it's currently refusing to give out docs for anything earlier than 4.5 (was working a few weeks ago, but now their Javascript always errors out :( ).

...what's the equivalent in 2.x? Or is there no way of doing this (sad face) ?


OR: am I missing something obvious about using Func? I get compiler errors that it's an undefined symbol, and I'm using the same context as I found in all the different online resources / examples :(

Was it helpful?

Solution

It's important to distinguish between language versions and platform versions... and likewise functionality. Currently you're mixing up various aspects.

Func and Action aren't part of the C# language at all - they're just delegate types which are part of the .NET framework. If you're using a version of the framework which doesn't include the delegate types you want, just declare your own. I'd suggest naming them differently to the .NET ones so that it won't matter if you then upgrade - there won't be any naming collisions. So:

public delegate void MyAction();
public delegate void MyAction<T>(T input);
... etc

Then once the relevant types are in place, you can use method group conversions as normal:

public void DoSomething(MyAction<string> action) { ... }

public void Foo(string text) { ... }

...

DoSomething(Foo);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top