Question: As noted, C# has unusually sophisticated support for firstclass subroutines. Among other things, it allows delegates to be instantiated from anonymous nested methods, and gives

As noted, C# has unusually sophisticated support for firstclass subroutines. Among other things, it allows delegates to be instantiated from anonymous nested methods, and gives local variables and parameters unlimited extent when theymay be needed by such a delegate. Consider the implications of these features in the following C# program:

using System; public delegate int UnaryOp(int n); // type declaration: Unary0p is a function from ints to ints public class Foo { static int a = 2; static Unary0p b(int c) { int d = a + c; Console.WriteLine (d); return delegate (int n) { return c + n; };What does this program print? Which of a, b, c, and d, if any, is likely to be statically allocated? Which could be allocated on the stack? Which would need to be allocated in the heap? Explain.

using System; public delegate int UnaryOp(int n); // type declaration: Unary0p is a function from ints to ints public class Foo { static int a = 2; static Unary0p b(int c) { int d = a + c; Console.WriteLine (d); return delegate (int n) { return c + n; }; public static void Main(string [] args) { Console.WriteLine (b(3) (4));

Step by Step Solution

3.33 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The program prints 5 7 Objects a and b are globally defined and could be stat... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!