Analysis of delegation and event examples in C #
The editor of this article introduces "delegation and event case analysis in C#" in detail, with detailed content, clear steps and proper handling of details. I hope this article "delegation and event case analysis in C#" can help you solve your doubts. Let's follow the editor's train of thought to learn new knowledge.
1. Define delegate delegate void StudentDelegate (); / / [1] define a delegate 2, define a class that invokes and define events / define events and invoke events must be placed in a class / class InvokeDefine / / [2] define a class {public event StudentDelegate StudentEvent that invokes and defines events Public void Invoke () {StudentEvent?.Invoke (); / / .Null check operator}} III, subscriber class EventFunction / / [3] subscriber {public void Student1 () {Console.WriteLine ("I am subscriber 1") } public void Student2 () {Console.WriteLine ("I am a subscriber 2");}} IV. Subscription event static void Main (string [] args) {EventFunction eventFunction = new EventFunction (); InvokeDefine invokeDefine = new InvokeDefine (); invokeDefine.StudentEvent + = eventFunction.Student1 / / [4] subscription event invokeDefine.StudentEvent + = eventFunction.Student2; invokeDefine.Invoke (); Console.ReadKey () } after reading this, the article "delegation and event example Analysis in C#" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.