Struct callback woes + interface

CSharp · Console

/Program.cs

using System;
using System.Threading.Tasks;
					
public class Program {
	class C {}
	
	interface I {
		public void PrintID();
	}
	
    struct S : I {
		public string id;
		C c;
		
		public S(C c, string id){
			this.id = id;
			this.c = c;
		}
		
		public void PrintID(){
			Console.WriteLine(string.Format("My ID = {0} and my c = {1}", id, c));
		}
	}
	
	public static void Main() {
		C c = new C();
		S s = new S(c, "default");
		s.id = "foo";
		ExecuteInTime(100, s.PrintID); //id == foo
		//ExecuteInTime(100, () => s.PrintID()); //id == bar
		s.id = "bar";
		
		// prevent Main() from exiting
		ExecuteInTime(500, () => {}).Wait();
	}
	
	public static async Task ExecuteInTime(int delay, Action callback) {
		await Task.Delay(delay);
		callback();
	}
}
;