public Form1() { InitializeComponent(); startBC(); graphOpened = false; expressionTextBox.Select(); grafoveOkno = null; InitializeTooltips(); pamet = new string[10]; delButtons = new Button[7]; for (int i = 0; i < 10; i++) { pamet[i] = "Memory " + (1 + i).ToString(); } memoryComboBox.SelectedIndex = 0; delButtons[0] = Del0;; delButtons[1] = Del1;; delButtons[2] = Del2;; delButtons[3] = Del3;; delButtons[4] = Del4;; delButtons[5] = Del5;; delButtons[6] = Del6;; Del0.Hide(); Del1.Hide(); Del2.Hide(); Del3.Hide(); Del4.Hide(); Del5.Hide(); Del6.Hide(); favButtons = new List <Button>(); }
static void Main(string[] args) { var obj = new Class1(); obj.X(); int n = 0; Work(ref n); var c = new Class1(); c.MyProperty2 = 90; Work2(ref c); int s; Work3(out s); int q = 0; int.TryParse("12312", out q); var db = DbManager.Instance; db.X(); //del.Work(new Class2()); Del2 <Class1> del2 = X; }
public void Met2() { Console.WriteLine($"Main() thread = {Thread.CurrentThread.ManagedThreadId}"); Del2 del2f1 = new Del2(f1); del2f1.BeginInvoke(null, null); Del2 del2f2 = new Del2(f2); del2f2.BeginInvoke(null, null); Del2 del2f3 = new Del2(f3); del2f3.BeginInvoke(null, null); Del2 del2f4 = new Del2(f4); del2f4.BeginInvoke(null, null); Del2 del2f5 = new Del2(f5); del2f5.BeginInvoke(null, null); Del2 del2f6 = new Del2(f6); del2f6.BeginInvoke(null, null); Del2 del2f7 = new Del2(f7); del2f7.BeginInvoke(null, null); Del2 del2f8 = new Del2(f8); del2f8.BeginInvoke(null, null); Del2 del2f9 = new Del2(f9); del2f9.BeginInvoke(null, null); Thread.Sleep(1000); Console.WriteLine($"Main() thread = {Thread.CurrentThread.ManagedThreadId}"); }
static void Main2() { Del2 o1 = (a, b) => a + b; Console.WriteLine(o1(1, 2)); Del3 o3 = (a, b) => b > a; Console.WriteLine(o3(1, 2)); Del4 obj4 = a => a.Basic; Console.WriteLine(obj4(new Emp { Basic = 1234 })); Del5 obj5 = a => a.Basic > 10000; Console.WriteLine(obj5(new Emp { Basic = 12345 })); Console.ReadLine(); }
static void Main(string[] args) { Del2 pro = (double x, int i) => { double res1 = 1; for (int j = 1; j < 6; j++) { res1 *= i * x / j; } return(res1); }; Del sum = (x) => { double res2 = 0; for (int i = 1; i < 6; i++) { res2 += pro(x, i); } return(res2); }; Console.Write("Write double x = "); double my_x; double.TryParse(Console.ReadLine(), out my_x); Console.WriteLine($"results = {sum?.Invoke(my_x)}"); Console.ReadKey(); }
static string decodeTransferEncoding(string s, Message m) { Dictionary <string, Delegate> d = new Dictionary <string, Delegate>(); Del2 quotedPrintableHandler = DecodeQuotedPrintable; Del2 base64Handler = decodeBase64; d.Add("base64", base64Handler); d.Add("quoted-printable", quotedPrintableHandler); string[] keywords = new string[] { "Content-Type: ", "\tcharset=", "Content-Transfer-Encoding: " }; foreach (string headerCheck in splitToLines(returnHeader(s))) { foreach (string keyword in keywords) { if (headerCheck.StartsWith(keyword)) { Console.WriteLine(keyword + " noted in " + headerCheck); foreach (KeyValuePair <string, Delegate> k in d) { if (headerCheck.Substring(keyword.Length) == k.Key) { return((string)k.Value.DynamicInvoke(s, m)); } } } } } return(s); }
static void Main(string[] args) { #region delegate Del del = Method4; del += Method1; //del(); Del del2 = new Del(Method4); del2 += Method4; //del2.Invoke(); var del3 = del + del2; //del3(); var del0 = new Del2(Method0); del0(new Random().Next(10, 50)); Action ActionDelegate = Method1; // del ActionDelegate(); Action <int> act = Method3; // void/ int // Action перегружается до 16 раз, как и Predicate и Func act(2); //Predicate<int> predicate; // тоже самое что и public delegate bool Predicate(int value); Func <int, int> func = Method0; // тоже самое что и public delegate last Func(first value); func?.Invoke(5); #endregion Person person = new Person() { Name = "Вася" }; person.GoToSleep += Person_GoToSleep; person.DoWork += Person_DoWork; person.TakeTime(DateTime.Parse("01.04.2018 18:05:30")); person.TakeTime(DateTime.Parse("02.04.2018 4:05:30")); Console.ReadLine(); }
private static Del2 CreateDelegateMethodReturnString() { // Instantiate the delegate. Del2 handler = TestString1; return(handler); }
public static int Main(string[] args) { // newobj Derived Derived d = new Derived(); // ldvirtftn Base::Foo // newobj Del1::.ctor Del1 b = new Del1(d.Foo); // ldftn Del1::Invoke // newobj Del2::.ctor Del2 f = new Del2(b.Invoke); // should call Derived.Foo not Base.Foo var r = f("Derived.Foo"); if (r != WasCalled.DerivedWasCalled) { return(1); } // should call Base.Foo not Derived.Foo var boundDelegate = (Del2)Activator.CreateInstance(typeof(Del2), b, typeof(Base).GetMethod(nameof(Base.Foo)).MethodHandle.GetFunctionPointer()); r = boundDelegate("Base.Foo"); if (r != WasCalled.BaseWasCalled) { return(2); } return(0); }
public StartScreen() { InitializeComponent(); // Instantiate the delegate. handler = makeVisible; join = joining; start = starting; invisible = makeInvisible; }
static void Main1() { //step 2 : create a delegate object the function name as a parameter Del1 objDel = new Del1(display); Del2 objDel2 = new Del2(display1); //step 3 : call the func via delegate obj objDel(); objDel2(1); }
static void Main5() { Del2 objDel2 = new Del2(Add); int ans; ans = objDel2(10, 20); Console.WriteLine(ans); Console.ReadLine(); }
public static void Test2() { Console.WriteLine("------------------------Test2--------------------------------"); Del2 del2 = (x, y) => x * y; Del2 del3 = (int x, int y) => x * y; Del2 del4 = (int x, int y) => { return(x * y); }; Console.WriteLine("Del2 del2 = (x,y) => x * y:{0}", del2(2, 3)); Console.WriteLine("Del2 del3 = (int x,int y ) => x * y:{0}", del3(2, 3)); Console.WriteLine("Del2 del4 = (int x,int y) => return x * y; :{0}", del4(2, 3)); Console.WriteLine("--------------------------------------------------------"); }
public void RemoveAt(int index) { if (this.InvokeRequired) { Del2 del = new Del2(RemoveAt); IAsyncResult result = this.BeginInvoke(del, new object[] { index }); this.EndInvoke(result); } else { Items.RemoveAt(index); } }
public static void Main() { Class1 c = new Class1(); Del del = new Del(c.Notify); del("Jack"); Del2 del2 = new Del2(c.Notify); del2("Happy"); c.CallBack(del2); Console.ReadKey(); }
public Timer(float time = 1, int numLoops = 1, Del2 up = null, Del end = null) { endTime = (time > 0) ? time : 1; // if this is zero, could loop indefinitely loops = numLoops; updateFunc = up; endFunc = end; paused = false; elapsedTime = 0; endless = (numLoops <= 0) ? true : false; TimerManager.AddTimer(this); }
static void Main(string[] args) { int a = 12345; int[] arr1 = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 99 }; int[] arr2 = { 11, 22, 30, 73, 50, 60, 70, 30, 78, 14 }; Del1 del1 = Class1.IntToArr; Del2 del2 = Class1.Print; del1.Invoke(a); del2.Invoke(del1.Invoke(a)); }
static void Main(string[] args) { Del1 d1 = Method1; Del2 d2 = Method2; int a = 39854; int[] arr1 = new int[] { 45, 23, 98, 23, 89, 34, 78, 53, 21, 12 }; int[] arr2 = d1?.Invoke(a); d2?.Invoke(arr1); d2?.Invoke(arr2); Console.WriteLine(d1.Target); Console.WriteLine(d1.Method); Console.WriteLine(d2.Target); Console.WriteLine(d2.Method); }
public static int Main(string[] args) { // newobj Derived Derived d = new Derived(); // ldvirtftn Base::Foo // newobj Del1::.ctor Del1 b = new Del1(d.Foo); // ldftn Del1::Invoke // newobj Del2::.ctor Del2 f = new Del2(b.Invoke); // should call Derived.Foo not Base.Foo var r = f("abcd"); return(r); }
static void Main(string[] args) { int[] nums = { 1, 5, 7, 234, 12, 5 }; Del del1 = Print; del1(nums); Console.WriteLine(); Del2 del2 = Reverse; del1(del2(nums)); Console.WriteLine(); del2 = Sort; del1(del2(nums)); }
static void Main(string[] args) { Del1 d = delegate() { Console.WriteLine("Display MEthod"); }; d(); Del2 d2 = delegate(int n) { Console.WriteLine($"number is {n}"); }; d2(5); Del3 d3 = delegate(string str) { return(str.ToUpper()); }; Console.WriteLine(d3("hello world")); Del4 d4 = delegate() { return(DateTime.Now.ToString()); }; Console.WriteLine(d4()); int x = 10; Del1 d5 = delegate() { Console.WriteLine(x); }; d5(); }
static void Main() { Del2 o = Display; o("a"); Del3 o3 = Add; int ans = o3(10, 20); Console.WriteLine(ans); Del1 o4 = class2.Show; o4(); class2 oCls2 = new class2(); Del1 o5 = oCls2.Show2; o5(); Console.ReadLine(); }
//lambda语句本质就是一个匿名函数 static void Main(string[] args) { //Del1 del1 = delegate() { Console.WriteLine("无参数,无返回值"); }; Del1 del1 = () => { Console.WriteLine("无参数,无返回值"); }; //Del2 del2 = delegate(string name){int a=name.Length ;Console .WriteLine ("有参数,字符串长{0}",a);}; Del2 del2 = (string name) => { int a = name.Length; Console.WriteLine("有参数,字符串长{0}", a); }; //Del3 del3 = delegate(string name) { int a = name.Length; return a; }; Del3 del3 = (string name) => { int a = name.Length; return(a); }; del1(); del2("zhangsan"); Console.WriteLine(del3("zhangsan")); //关于Lambda在参数中的应用 List <int> listNums = new List <int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, }; listNums.RemoveAll(n => n > 7);//见提示Precidate <int> match,就是使用lambda输入一个条件; foreach (int item in listNums) { Console.WriteLine(item); } IEnumerable <int> aa = listNums.Where(n => n > 3);//根据提示写的返回值类型 foreach (int item in aa) { Console.WriteLine(item); } Console.ReadKey(); }
public void NullTarget_Static() { Del2 d = (Del2)Delegate.CreateDelegate(typeof(Del2), null, typeof(DelegateTest).GetMethod("method2")); Assert.AreEqual(10, d(5)); }
static void Main(string[] args) { #region MyRegion Del handler = DelegateMethod; handler("Hello"); MethodWithCallback(1, 2, handler); MethodWithCallback(3, 4, handler); MethodWithCallback(5, 6, handler); MethodClass obj = new MethodClass(); Del d1_1 = obj.Metrhod1; Del d1_2 = obj.Metrhod2; Del d1_3 = DelegateMethod; Del allMethodsDelegate = d1_1 + d1_2; allMethodsDelegate += d1_3; int invocationCount = allMethodsDelegate.GetInvocationList().Length;//.GetLength(0); Console.WriteLine($"count:{invocationCount}"); Del2 d2 = obj.MultipyNumbers; Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':"); for (int i = 1; i <= 5; i++) { d2(i, 2); } Console.WriteLine("委托映射到静态方法和实例方法,并返回来自两种方法的具体信息:"); Del3 d3_1 = obj.InstancaMethod; d3_1(); Del3 d3_2 = MethodClass.StaticMethod; d3_2(); Console.WriteLine("multicast delegates(多播委托):"); CustomDel hiDel, byeDel, multiDel, multiMinusHiDel; hiDel = Hello; byeDel = Goodbye; multiDel = hiDel + byeDel; multiMinusHiDel = multiDel - hiDel; Console.WriteLine("Invoke delegate hiDel:"); hiDel("A"); Console.WriteLine("Invoke delegate byDel:"); byeDel("B"); Console.WriteLine("Invoke delegate multiDel:"); multiDel("C"); Console.WriteLine("Invoke delegate multiMinusHiDel:"); multiMinusHiDel("D"); #endregion #region Func Delegate Console.WriteLine("\nFunc Delegate:\n"); OutPutTarget output = new OutPutTarget(); // 调用自定义委托:WriteMethod //WriteMethod methodCall = output.SendToFile; // 调用Func<TResult> //Func<bool> methodCall = output.SendToFile; // 使用 Func<TResult> 委托与 C# 中的匿名方法 //Func<bool> methodCall = delegate () { return output.SendToFile(); }; // lambda 表达式 Func<T, TResult> 委托 Func <bool> methodCall = () => output.SendToFile(); if (methodCall()) { Console.WriteLine("Success!"); } else { Console.WriteLine("File write operation failed"); } Console.WriteLine("\nFunc Delegate示例:\n"); LazyValue <int> lazyOne = new LazyValue <int>(() => ExpensiveOne()); LazyValue <long> lazyTwo = new LazyValue <long>(() => ExpensiveTwo("hello")); Console.WriteLine("LazyValue objects has been created."); Console.WriteLine(lazyOne.Value); Console.WriteLine(lazyTwo.Value); #endregion #region Func<T1, T2, TResult> 委托 Console.WriteLine("Func<T1, T2, TResult> 委托:"); Func <string, int, bool> predicate = (str, index) => str.Length == index; string[] words = { "orange", "apple", "Article", "elephant", "star", "and" }; IEnumerable <string> aWords = words.Where(predicate).Select(str => str); foreach (string word in aWords) { Console.WriteLine(word); } #endregion }
static void Main(string[] args) { List <Lager> lager = new List <Lager>(); var ergebnisLager100 = lager.Where(l => l.Lagerbestand > 100); Lager l1 = new Lager(); l1.OnLagerUberschritten += LagerUeberschritten; //l1.EventLagerUeberschritten += LagerUeberschritten; l1.AddToLager(70); //70 //if (l1.Lagerbestand>100) l1.AddToLager(30); //100 //if (l1.Lagerbestand > 100) l1.AddToLager(20); //120 //if (l1.Lagerbestand > 100) l1.Print(); IPrintable iPrint = l1; iPrint.Print(); l1.GetLagerwert(12); //l1.Release(); //l1.Close(); //l1.Kill(); l1.Dispose(); l1 = null; //Garbage -- GC //Temp Resource-Usage-Pattern using (Lager l = new Lager()) { l.Lagerbestand = 120; l.AddToLager(20); }//Dispose in einem finally-Block if (iPrint is Lager) { Lager lager1 = (Lager)iPrint; } //Direkter Aufruf M1(); //Delegate, FunctionPoint Del1 d = M1; d(); Del2 d2 = M4; d2("Johann"); //Generische Delegates Func mit Return, Action void Action a1 = M1; a1(); Action <string> a2 = M4; a2("Johann"); Func <string, int> f = M3; var erg = f("Hans"); }
public void Remove() { TimerManager.RemoveTimer(this); updateFunc = null; endFunc = null; }
static int CallMathOperation(Del2 objMathOperation, int a, int b) { //return Add(a, b); //return Subtract(a, b); return(objMathOperation(a, b)); }
public static void Time(this Del2 action) { // Logic to time the action action(); }
private void CallBack(Del2 del) { }
private void Foo2(Del2 del) { var res1 = del(11); var res2 = del.Invoke(22); }