C# Querying With LINQ Functional Programming in C# Actions and Funcs

Taras Metelskii
Taras Metelskii
3,585 Points

Why this doesn't work? FUNC

So i tried to do this and it doesn't work:

using System;
namespace Treehouse.CodeChallenges
{
    public class Program
    {
        public Func<int, int> Square = delegate(int number){
            return number*number; };
    }
}

But this :

using System;
namespace Treehouse.CodeChallenges
{
    public class Program
    {
        public Func<int, int> Square = delegate(int number){
            return number * number; };
    }
}

Worked well. And the only difference is the space between number and *.

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! I think the answer might be a little out of the scope of this course, but let me say that the asterisk is a special operator. Not only is it used for common multiplication, but it's also used in references to something we call pointers. These are things that point to a physical address in memory. And they are written like myVariable*. This is not only true for C# but also ANSI C and likely other languages as well.

My best guess here, is that the compiler thought you were trying to reference one of these pointers instead of doing normal multiplication. Going forward, be aware that if you are intending to do multiplication put spaces before and after the asterisks. You can view some information on pointers in C# from this MSDN documentation

Hope this helps! :sparkles:

Taras Metelskii
Taras Metelskii
3,585 Points

So i guessed that " * " is unary operator. And if i tried to pass more then one variable compilator will do all work for me. Thanks for your answer.