Exercise 3-6 (BluffingStrategy delegate)

Chapter_3     Exercise_3-5 Exercise_3-7







Exercise 3-6     TCS, p. 85


Exercise 3-6. Turn the sample code that defines the BluffingStrategy delegate and use the method SweetPete.SmilePleasantly() to instantiate the delegate into a program that compiles and runs.




BluffingStrategy.cs     TCS, p. 61-62


public class BluffingStrategyTest
{
public class PokerHand {}

delegate void BluffingStrategy(PokerHand x);

public class BlackBart
{
public void SnarlAngrily(PokerHand y)
{System.Console.WriteLine("Snarling");}
public int AnotherMethod(PokerHand z){return 0;}
}
public class SweetPete
{
public void YetAnother(){}
public static void SmilePleasantly(PokerHand z)
{System.Console.WriteLine("Smiling");}
}

public static void Main()
{
PokerHand ph = new PokerHand();

BlackBart b = new BlackBart();
BluffingStrategy bs = new BluffingStrategy(b.SnarlAngrily);
bs(ph);
b.SnarlAngrily(ph);

bs = new BluffingStrategy(SweetPete.SmilePleasantly);
bs(ph); // call static function from a static context, Main()
SweetPete.SmilePleasantly(ph); // call static function
}
}
/*
mcs BluffingStrategy.cs
mono BluffingStrategy.exe
Snarling
Snarling
Smiling
Smiling
*/









Chapter_3     Exercise_3-5 BACK_TO_TOP Exercise_3-7



Comments

Popular posts from this blog

Contents