Microsoft .NET 면접 질문과 답변
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); } } } |
복습용 저장
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
도움이 되었나요? 예 아니요
Most helpful rated by users:
- Name 10 C# keywords.
- What is public accessibility?
- .NET Stands for?
- What is private accessibility?
- What is protected accessibility?