static void Main(string[] args) { //1. Func to delegates string F(dynamic s) { Console.WriteLine(s); return(s); } Func <string, string> f = F; //new A(f);//Error CS1503 Argument 1: cannot convert from 'System.Func<string, string>' to 'ConsoleApp3.Program.A.D' new A(new A.D(f)); //I'm A! new A(x => f(x)); //I'm A! Func <string, string> f2 = s => { Console.WriteLine(s); return(s); }; //new A(f2);//Same as A(f) new A(new A.D(f2)); //I'm A! new A(x => f2(x)); //I'm A! //You can even convert between delegate types new A(new A.D(new B.D(f))); //I'm A! //2. delegate to F A.D d = s => { Console.WriteLine(s); return(s); }; Func <string, string> f3 = d.Invoke; f3("I'm f3!"); //I'm f3! Func <string, string> f4 = new Func <string, string>(d); f4("I'm f4!"); //I'm f4! Console.ReadLine(); }
public B() { string error = ""; if (typeof (C) != typeof (A.B.C)) error += " 'typeof' keyword,"; object o0 = new C (); if (o0.GetType() != typeof (A.B.C)) error += " 'new' keyword,"; C o1 = new C (); if (o1.GetType () != typeof (A.B.C)) error += " type declaration,"; object o2 = new A.B.C (); if (!(o2 is C)) error += " 'is' keyword,"; object o3 = o2 as C; if (o3 == null) error += " 'as' keyword,"; try { object o4 = (C) o2; } catch { error += " type cast,"; } try { object o5 = (C) (o2); } catch { error += " invocation-or-cast,"; } object o6 = new C [1]; if (o6.GetType ().GetElementType () != typeof (A.B.C)) error += " array creation,"; if (typeof (C []).GetElementType () != typeof (A.B.C)) error += " composed cast (array),"; ArrayList a = new ArrayList (); a.Add (new A.B.C ()); try { foreach (C c in a) { } } catch { error += " 'foreach' statement,"; } if (error.Length != 0) throw new Exception ("The following couldn't resolve C as A+B+C:" + error); }
static void Main(string[] args) { A a = new A(); B b = new B(); C c = new C(); A.D d1 = new A.D(b.m2); //singlecast delegate A.D d2 = new A.D(c.m3); //singlecast delegate A.D d3 = new A.D(c.m4); //single cast deligate //a.m1(d1); A.D d4 = d1 + d2 + d3; //Multicast deligate a.m1(d4); Console.ReadLine(); }
public B() { using (C c = new C()) { } }
public static void Main() { object o = new C(); }
public B() { string error = ""; if (typeof(C) != typeof(A.B.C)) { error += " 'typeof' keyword,"; } object o0 = new C(); if (o0.GetType() != typeof(A.B.C)) { error += " 'new' keyword,"; } C o1 = new C(); if (o1.GetType() != typeof(A.B.C)) { error += " type declaration,"; } object o2 = new A.B.C(); if (!(o2 is C)) { error += " 'is' keyword,"; } object o3 = o2 as C; if (o3 == null) { error += " 'as' keyword,"; } try { object o4 = (C)o2; } catch { error += " type cast,"; } try { object o5 = (C)(o2); } catch { error += " invocation-or-cast,"; } object o6 = new C [1]; if (o6.GetType().GetElementType() != typeof(A.B.C)) { error += " array creation,"; } if (typeof(C []).GetElementType() != typeof(A.B.C)) { error += " composed cast (array),"; } ArrayList a = new ArrayList(); a.Add(new A.B.C()); try { foreach (C c in a) { } } catch { error += " 'foreach' statement,"; } if (error.Length != 0) { throw new Exception("The following couldn't resolve C as A+B+C:" + error); } }
public B () { using (C c = new C ()) { } }
public static void Main() { object o = new C(); }