-5

I am confronted with a task from an old exam, and I have no idea what it is about:

class Demo
{ 
    satic void Main()
    {
        Func <int,int>  xxx = null;
        xxx = n => n <=  1 ? 1:n * xxx(n-1);

        for (int m=1; m < 6; m++)
        {   
            Console.WriteLine("m=  {0}  xxx = {1}", m, xxx(m));
        }
    }
}

There is some generic construct with type int,int but what is this? There is an if-else statement, but what is happening here? Especially with the => operator? I would be very thankful if someone could tell me what is happening here.

We should correct this code and write down the print out. But we do not know what happens in the first lines.

Update: In Visual Studio, no errors are found. So why should we find an error then? The output is the following:

  m=1 xxx=1
  m=2 xxx=2
  m=3 xxx=6
  m=4 xxx=24
  m=5 xxx=120

It is still unclear to me what is happening.

jvdhooft
  • 657
  • 1
  • 12
  • 33
Julian Herbold
  • 537
  • 9
  • 20
  • Have you actually tried moving the text cursor onto "=>" and hitting F1 (in Visual Studio)? –  Jun 29 '14 at 15:45
  • Why don't write it to Visual Studio, compile, fix a few minor syntax error and see the output. (BTW It is a [recursive lambda](http://blogs.msdn.com/b/madst/archive/2007/05/11/recursive-lambda-expressions.aspx)) – L.B Jun 29 '14 at 15:45
  • the mistakes in code came from transfering it from a picture. there isnt any mistake so what should we correct then, lol – Julian Herbold Jun 29 '14 at 15:55
  • I'll laugh more while you are taking your exam, lol. – L.B Jun 29 '14 at 16:01

1 Answers1

1
  1. Your code is full of typos and missing characters; These should be easy to find when using a compiler or DotNetFiddle.

  2. What does it do? It prints out the m! (factorial of m) for 1..5

  3. the two lines of interest:

    Func <int,int> xxx = null;

Declares a new Func Delegate with one parameter of type int which returns an int. There is no functionality bound yet to this delegate (ie. it is null). You may consider such a delegate as something like a function pointer in c++. Thus, it's more or less a variable holding a function (see MSDN ) which can be called like xxx(m) for an integer m.

xxx = n => n <=  1 ? 1:n * xxx(n-1);

Assigns functionality to the delegate xxx via a Lambda expression. (see MSDN )

xxx = is the assgigment and n => ... is the lambda expression, which basically says, for an input parameter n return the following

n <= 1 ? 1:n * xxx(n-1); is the functionality to be executed for n. Which may also be written as

xxx = n => {
        if (n <=1) return 1;
        return n* xxx(n-1);

    };

As xxx is already defined in the line above, it can be called here like every other method.

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • thank you, your answer was perfect.but still, there isnt any mistake in the code when running in vs. So there is nothing I can correct, or not? – Julian Herbold Jun 29 '14 at 19:18
  • In your original unedited question (that I was referring to in my answer) there were the following errors: `satic void Main()` should be `static void Main(string[] args)` (ie. missing character, missing parameters), `xxx = n => n <= 1 ? 1:n * xxx(n-1)` (missing semicolon) and `Console.WirteLine` should be `Console.WriteLine` (type). There is no way, Visual Studio won't have displayed those errors and have successully executed your code. – derpirscher Jun 29 '14 at 19:47
  • yeah sorry for the confusion. that were my mistakes because i had to write the code per hand from the original (photo). the original code compiles without mistakes. maybe our prof wants to confuse us in addition – Julian Herbold Jun 29 '14 at 20:05