public static void Example() { del1 = new StringProcessor(DelegateManipulation.delegateCompatible1); del2 = new StringProcessor(DelegateManipulation.delegateCompatible2); //New Delegate is created each time del1 = (StringProcessor)Delegate.Combine(del1, del2); // Cast is required del1 += del2; // Cast is not required del1 = del1 + del2; // Cast is not required /* * displays * MethodOne * MethodTwo * MethodTwo * MethodTwo */ // To Find out how many methods is going to be called int invocationCount = del1.GetInvocationList().GetLength(0); del1 = (StringProcessor)Delegate.Remove(del1, del2); // Cast is required del1 -= del2; // Cast is not required del1 = del1 - del2; // Cast is not required }
public void VariousWaysToInvokeDelegate() { StringProcessor del = DelegateActions.ToUpperCaseStatic; var input = "test"; var expectedOutput = "TEST"; var directCall = del(input); Assert.AreEqual(expectedOutput, directCall); var standardInvoke = del.Invoke(input); Assert.AreEqual(expectedOutput, standardInvoke); var dynamicInvoke = del.GetInvocationList()[0].DynamicInvoke(input); Assert.AreEqual(expectedOutput, dynamicInvoke); var asyncResult = del.BeginInvoke(input, null, null); var asyncInvoke = del.EndInvoke(asyncResult); Assert.AreEqual(expectedOutput, asyncInvoke); }
static void Main(string[] args) { EnumSample es = new EnumSample(); es.Test(); System.Console.WriteLine(new structSample(12, 22).Diagnoal); structSample ss = new structSample(1, 2); // ss.Length = 11; //ss.Width = 22; System.Console.WriteLine(ss.Diagnoal); System.Console.WriteLine(StaticClass.cc); Person jon = new Person("Jon"); StringProcessor jonsVoice, tomsVoice, background; jonsVoice = new StringProcessor(jon.Say); { Person tom = new Person("Tom"); tomsVoice = new StringProcessor(tom.Say); } background = new StringProcessor(Background.Note); jonsVoice("Hello, son."); tomsVoice("hello, Daddy"); background("An airplane flies past"); jon.Name = "Jon Snow"; jonsVoice += tomsVoice; foreach (Delegate dele in jonsVoice.GetInvocationList()) { dele.DynamicInvoke("Iknow nothing yegret"); } jonsVoice("I know nothing"); background.Invoke("Another airplane files past"); ClassSizeof szof = new ClassSizeof(name: "zhouwei", age: 33, gender: "male"); // Program pro = new Program(); //class Console.WriteLine("{0}-{1}-{2}", szof.Name, szof.Age, szof.Gender); Console.WriteLine(Marshal.SizeOf(szof /*new ClassSizeof(name: "zhouwei", age: 33, gender: "male")*/)); List <ClassSizeof> list = new List <ClassSizeof> { new ClassSizeof(name: "zhouwei", age: 33, gender: "male"), new ClassSizeof(name: "zhouchao", age: 30, gender: "male"), new ClassSizeof(name: "zhoujiazu", age: 230, gender: "male"), new ClassSizeof(name: "dengmin", age: 32, gender: "female") }; list.Sort(new Comparer()); list.Sort(delegate(ClassSizeof s1, ClassSizeof s2) { return(s2.Age - s1.Age); }); list.Sort(comparison: (a, b) => { return(a.Age - b.Age); }); foreach (ClassSizeof szof2 in list.OrderBy(z => z.Name)) { Console.WriteLine("{0}-{1}-{2}", szof2.Name, szof2.Age, szof2.Gender); } foreach (ClassSizeof szof1 in list) { Console.WriteLine("{0}-{1}-{2}", szof1.Name, szof1.Age, szof1.Gender); } Predicate <ClassSizeof> gr30 = delegate(ClassSizeof sz) { return(sz.Age > 30); }; List <ClassSizeof> qualified = list.FindAll(gr30); Action <ClassSizeof> print = Console.WriteLine; Console.WriteLine("action print"); qualified.ForEach(print); var qua = from ClassSizeof sz in list where sz.Age > 30 select sz; Console.WriteLine("LINQ"); foreach (ClassSizeof sz in qua.OrderBy(p => p.Age)) { Console.WriteLine(sz); } DelegateEventsTest det = new DelegateEventsTest(); det.Test(); GenericDictionary td = new GenericDictionary(); td.Test(); PartialClassSampleInvoker pcsi = new PartialClassSampleInvoker(); pcsi.Test(); ExtensionClass ec = new ExtensionClass(); ec.Test(); OverrideAndHiddenSample oahs = new OverrideAndHiddenSample(); oahs.Test(); AsAndIsSample aais = new AsAndIsSample(); aais.Test(); WeakReferenceSample srs = new WeakReferenceSample(); srs.Test(); LinkedListSample lls = new LinkedListSample(); lls.Test(); CoContraVarianceIsMakeSureConvertFromDerivedToBase iovs = new CoContraVarianceIsMakeSureConvertFromDerivedToBase(); iovs.Test(); Console.Read(); }