Microsoft .NET Interviewfragen und Antworten
Question: Write some code to use a delegate.Answer: Member function with a parameterusing System; namespace Console1 { class Class1 { delegate void myDelegate(int parameter1); static void Main(string[] args) { MyClass myInstance = new MyClass(); myDelegate d = new myDelegate(myInstance.AMethod); d(1); // <--- Calling function without knowing its name. Test2(d); Console.ReadLine(); } static void Test2(myDelegate d) { d(2); // <--- Calling function without knowing its name. } } class MyClass { public void AMethod(int param1) { Console.WriteLine(param1); } } } Multicast delegate calling static and member functions using System; namespace Console1 { class Class1 { delegate void myDelegate(int parameter1); static void AStaticMethod(int param1) { Console.WriteLine(param1); } static void Main(string[] args) { MyClass myInstance = new MyClass(); myDelegate d = null; d += new myDelegate(myInstance.AMethod); d += new myDelegate(AStaticMethod); d(1); //both functions will be run. Console.ReadLine(); } } class MyClass { public void AMethod(int param1) { Console.WriteLine(param1); } } } |
Zum Wiederholen speichern
Speichere diesen Eintrag als Lesezeichen, markiere ihn als schwierig oder lege ihn in einem Wiederholungsset ab.
Melde dich an, um Lesezeichen, schwierige Fragen und Wiederholungssets zu speichern.
Ist das hilfreich? Ja Nein
Am hilfreichsten laut Nutzern:
- Name 10 C# keywords.
- What is public accessibility?
- .NET Stands for?
- What is private accessibility?
- What is protected accessibility?