Les méthodes anonymes en C#

Comme son nom l’indique, une méthode anonyme est une méthode sans nom. Les méthodes anonymes en C# peuvent être définies à l’aide du mot clé delegate et peuvent être affectées à une variable de type délégué.
 
 

Exemple :
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
public delegate void Show(string str);
namespace WayToLearnX {
public class DelegateApp {
public static void Main(string[] args)
{
Show show = delegate(string str) {
Console.WriteLine("Welcom to {0}", str);
};
show("WayToLearnX!");
}
}
}
using System; public delegate void Show(string str); namespace WayToLearnX { public class DelegateApp { public static void Main(string[] args) { Show show = delegate(string str) { Console.WriteLine("Welcom to {0}", str); }; show("WayToLearnX!"); } } }
using System;

public delegate void Show(string str);

namespace WayToLearnX {
   
   public class DelegateApp {
	   
		public static void Main(string[] args)
		{
			Show show = delegate(string str) { 
			      Console.WriteLine("Welcom to {0}", str); 
			};

			show("WayToLearnX!");
		}
   }
}

La sortie :

Welcom to WayToLearnX!
Les délégués en C#

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *