Is it possible to add ToString methods to an array of delegates?? If so how?? here is the code I have written:
public delegate string Task();
public static void Main(string[] args)
{
ArrayList studentArray = new ArrayList();
Course italianCook = new ItalianCookCourse { Teacher = "Ben Hodd" };
Course seafoodCook = new SeafoodCookCourse { Teacher = "Harry Cotter"};
Course sewingCourse = new SewingCourse
{
Teacher = "Margaret Mair",
ChargePerStudent = scFee,
CostPerStudent = 100.00m,
};
Course creativeWrite = new CreativeWritCourse { Teacher = "Mary Smith };
Course businessWrite = new BusinessWritCourse { Teacher = "Mary Smith" };
Task[] tasks = new Task(italianCook.ToString, seafoodCook.ToString, sewingCourse.ToString);
The error message is "string Class.ToString()" - "Method name expected"
As the tostring method handle as a string, is it possible to add it to delagate??
Assuming you fixed up the syntax, the solution would be to remove the ()'s from the end of the ToString's. To create a delegate based on a method, you only need to give its name.
Task[] tasks = new Task[] { Class1.ToString, Class2.ToString }
Note that the above code does not actually compile. ToString() is not a static method, so you need to pass in an object reference along with the function name:
object o = new object();
Task[] tasks = new Task[] { o.ToString };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.