Advertisement
fcamuso

Delegate - A

Mar 14th, 2021
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2.  
  3. namespace delegate_1
  4. {
  5.  
  6.   //delegate void LogDelegate(string dati);
  7.  
  8.   class Transazione
  9.   {
  10.     LogDelegate metodoLog = null;
  11.    
  12.     public Transazione(LogDelegate metodo  )
  13.  
  14.     { metodoLog = metodo; }
  15.  
  16.     public void RichiedeControllo()
  17.     {
  18.       Controllo();
  19.     }
  20.    
  21.     void Controllo()
  22.     {
  23.       bool condizione = true;
  24.       // ... controlli
  25.  
  26.       if (condizione)
  27.         metodoLog("dati");
  28.     }
  29.  
  30.    }
  31.  
  32.  
  33.   class Program
  34.   {
  35.     static void UnTipoDiLog(string dati)
  36.     { Console.WriteLine(dati);}
  37.      
  38.     static void UnAltroTipoDiLog(string dati)
  39.     { Console.WriteLine($"----- {dati} ----------"); }
  40.  
  41.     static void Main(string[] args)
  42.     {
  43.  
  44.       Transazione tipo1 = new Transazione(UnTipoDiLog);
  45.       Transazione tipo2 = new Transazione(UnAltroTipoDiLog);
  46.  
  47.       tipo1.RichiedeControllo();
  48.       tipo2.RichiedeControllo();
  49.        
  50.  
  51.     }
  52.   }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement