- .
, , System.Delegate, , - , .
:
public static String Meow(Cat cat) { return "meow"; }
public static String Purr(Cat cat) { return "purr"; }
delegate String CatSound(Cat cat);
CatSound[] catSounds = new CatSound[] {
Meow,
Purr
};
:
Cat orion = new Cat();
catSounds[0]( orion ); // meow
catSounds[1]( orion ); // purr
DogSound , : Delegate[]...
delegate String DogSound(Dog dog);
Delegate[] petSounds = new Delegate[] {
new CatSound( Meow ),
new CatSound( Purr ),
new DogSound( Woof ),
new DogSound( Bark ),
};
... DynamicInvoke (https://msdn.microsoft.com/en-us/library/system.delegate.dynamicinvoke(v=vs.110).aspx), , , , , MemberAccessException.
Dog pupper = new Dog();
Cat orion = new Cat();
petSounds[0].DynamicInvoke( orion );
petSounds[1].DynamicInvoke( orion );
petSounds[2].DynamicInvoke( pupper );
petSounds[3].DynamicInvoke( orion );
delgates "a interface ".
.NET Framework 3.5 delegate ( , delegate # 3.0), System.Action System.Func, 95% , . , delegate CatSound , Func<Cat,String>.