How to use delegates in correct way / Understanding delegates

后端 未结 3 1664
日久生厌
日久生厌 2020-12-18 06:07

use - C# (.Net Framework 4.5, Visual Studio 2012)

I try to understand such theme like Delegate, and currently I have few points, that must be clarified for me. I fo

3条回答
  •  一生所求
    2020-12-18 06:56

    Delegate declarations can be placed in any source file with public and internal accessibility. I personally place them at the top of a class source for which it is most applicable.

    For instance if I have a specialized event delegate that takes CustomEventArgs as a parameter, I will put the delegate declaration at the top of that file:

    namespace MyNamespace {
       public delegate void SpecialEventDelegate(object sender, CustomEventArgs e);
    
       public class CustomEventArgs : EventArgs {
          // implementation details
       }
    }
    

    Another option is to put delegates together in a single source file ... I haven't seen this done and would be subject to coding guidelines.

    A delegate generally has two qualifications:

    • A declaration of a method or class member
    • A reference to a method or class member

    The first qualification is the declaration of a delegate:

       public delegate void SpecialEventDelegate(object sender, CustomEventArgs e);
    

    ... or using Func or Action:

       Action // similar declaration to the SpecialEventDelegate above
    

    Since an event handler (declared delegate) generally does not have a return type (void), a Func would not be used. A Func delegate requires a return type:

       Func // a delegate that return true/false
    

    See the linked MSDN articles for more on Func and Action. I only included references to these for completeness and insight on newer declaration methods. Func and Action are specialized wrappers for the delegate.

    The second qualification for a delegate is a reference to a method or class member. I may have a private method that acts as a handler for specific needs. Say a FileIO object needs a specific file handler for different types of files - that is, .XML, .TXT, .CSV:

    namespace MyNamespace {
    
       public delegate Stream OpenFile(FileInfo FileSpec);
    
    }
    

    Now any object can implement it's own OpenFile definition based on the file type but must return a Stream object.

    class XMLHandler : IFileHandler {
    
       private OpenFile xmlFileReader;
    
       // implementation of interface
       public OpenFile FileHandler {
          get { return xmlFileReader; }
       }
    
       public XMLHandler(){
          xmlFileReader = MyXmlFileReader;  // references the private method in this class
       }
    
       private Stream MyXmlFileReader(FileInfo XmlFileSpec) {
          // implementation specific to this class
       }
    
    }
    
    interface IFileHandler {
    
       OpenFile FileHandler { get; }
    
    }
    

    By using both a delegate declaration and an interface, we can pass the XMLHandler object as an IFileHandler and only expose the delegate via the FileHandler property without exposing the entire object. Note that the method referenced by the delegate is private. This is a special benefit of the delegate (and Func, Action).

提交回复
热议问题