Is there any trick that allow us to use coroutines with anonymous functions in C#?

Create a new method for each coroutine in C# is very boring. In Javascript I could use anonymous functoins to start a coroutine. But in C# I found no way to reproduce this code.

StartCoroutine(function() {
   // ... do something
  yield WaitForSeconds(1)
  // .. do one more thing
}());

I found this ask in stackoverflow, that could explains why not works, but if works for javascript, could exists a workaround.
In C#, why can’t an anonymous method contain a yield statement?.

– Edited

This question is not about migrate code from UnityScript to C#, but trying to discover a nice way to implements very asynchronous code without create a lot of functions.

Give this crazy complex example below. It is easy to implement and clear to understand a lot of “paralelism”. I can’t see any way to implement stuff like that in C# without create at least 3 functions (one for each coroutine).

StartCoroutine(function() {
   // wait user to achieve level 2
   while (level < 1) yield 0;
   
   // give some time
   yield WaitForSeconds(3);

   // show a nice label
   StartCoroutine(function () {
         yield label.FadeIn();
         labelShowingAudio.Play();
         yield WaitForSeconds(3);
         yield label.FadeOut();
         label.Hide();
    }());

    StartCoroutine(function () {
        player.anim['dance'].Play();
        yield WaitForSeconds(player.anim['dance'].length);
        enemy.KillPlayer();
    }());

    while (level < 2) yield 0;

    .
    .
    .
}());

I think you can achieve your goal with a few helper functions that allow chaining coroutines and wrapping an Action with a coroutine:

using UnityEngine;
using System;
using System.Collections;

public static class CoroutineUtils {

    /**
     * Usage: StartCoroutine(CoroutineUtils.Chain(...))
     * For example:
     *     StartCoroutine(CoroutineUtils.Chain(
     *         CoroutineUtils.Do(() => Debug.Log("A")),
     *         CoroutineUtils.WaitForSeconds(2),
     *         CoroutineUtils.Do(() => Debug.Log("B"))));
     */
    public static IEnumerator Chain(params IEnumerator[] actions) {
        foreach (IEnumerator action in actions) {
            yield return SomeSingletonGO.instance.StartCoroutine(action);
        }
    }

    /**
     * Usage: StartCoroutine(CoroutineUtils.DelaySeconds(action, delay))
     * For example:
     *     StartCoroutine(CoroutineUtils.DelaySeconds(
     *         () => DebugUtils.Log("2 seconds past"),
     *         2);
     */
    public static IEnumerator DelaySeconds(Action action, float delay) {
        yield return new WaitForSeconds(delay);
        action();
    }

    public static IEnumerator WaitForSeconds(float time) {
        yield return new WaitForSeconds(time);
    }

    public static IEnumerator Do(Action action) {
        action();
        yield return 0;
    }
}

Hope this helps.

No, C# does not allow yield expressions inside lambdas or anonymous methods.

http://blogs.msdn.com/b/ericlippert/archive/2009/08/24/iterator-blocks-part-seven-why-no-anonymous-iterators.aspx

[Answer copied from lordofduct on Unity Forums for the sake of anyone else having trouble finding the answer buried in the comments of the answers here]

Suggest for nearest alternative stuff:

void Main () {
    screenSwapper.StartCoroutine (
        WaitAndDo(yourTime, ()=>{
           // do anything
        })
    );
}

IEnumerator WaitAndDo (float time, Action action) {
	yield return new WaitForSeconds (time);
	action();
}

Here’s a series of Blog posts by one of the best known .Net framework archtiects, explaining why you can’t yield in certain situations: In C#, why can't an anonymous method contain a yield statement? - Stack Overflow

Definitely worth a read. The very first post neatly describes why unity coroutines are such a mess…

This is what I have been doing.
Always remember when in doubt consider an extension method.

Usage in a behavior is this.StartCoroutine( ()=> { your code here… } );

public static class MonoBehaviourExtension
 {
        public static void StartCoroutine(this MonoBehaviour mb, Action funcs)
        {
            mb.StartCoroutine(CoroutineRunnerSimple(new Action[] { funcs }));
        }

        public static void StartCoroutine(this MonoBehaviour mb, params Action[] funcs)
        {
            mb.StartCoroutine(CoroutineRunnerSimple(funcs));
        }
        private static System.Collections.IEnumerator CoroutineRunnerSimple(Action[] funcs)
        {
            foreach (var func in funcs)
            {
                if (func != null)
                    func();

                // yield return new WaitForSeconds(.01f);
                // Thanks bunny83
                yield return null;
            }
        }
    }