What can I replace here since I can't have a lambda function with implicit variable in C#?

Related searches

I'm trying to implement a tile-based platforming system for a 2D game demo from this C++ code into C#,

This is what I have written, It doesn't compile though due to the var variable usage.

 class GameLogic
{
    // Level storage
    String sLevel;
    int nLevelWidth;
    int nLevelHeight;

    // Player Properties
    float fPlayerPosX = 1.0f;
    float fPlayerPosY = 1.0f;
    float fPlayerVelX = 0.0f;
    float fPlayerVelY = 0.0f;
    bool bPlayerOnGround = false;

    // Camera properties
    float fCameraPosX = 0.0f;
    float fCameraPosY = 0.0f;

    public GameLogic()
    {
         var GetTile = (ref int x, ref int y) =>
        {
            if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
                return sLevel[y * nLevelWidth + x];
            else
                return "";
        };

        var SetTile = (ref int x, ref int y, string c)=>                
        {
            if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
                sLevel[y * nLevelWidth + x] = c;
        };

    }
}

What do you suggest I should do here, to implement this functionality. Thank you for your time.


It doesn´t compile because of the var statement. When you use LambdaExpressions and you want to save it to a variable you have to define the delegate Type. But you can´t use ref variables here. But that is ok, because you don´t change the value of x or y.

You need a Delegate that describes the type of your function. You could build you own delegate type but in C# there are two built in types for that case. Action & Action<T1-16> -> always returns void Func<T1-17> you can define the return type

The GetTile function takes two int and returns a string. So you can use Func<int, int, string>. The SetTitle function has one string parameter more but no return type. That would be Action<int, int, string>.

internal class GameObject
{
    private int nLevelWidth;
    private int nLevelHeight;

    public GameLogic()
    {
        Func<int, int, string> GetTile = (int x, int y) =>
        {
            if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
                return sLevel[y * nLevelWidth + x];
            else
                return "";
        };

        Action<int, int, string> SetTile = (int x, int y, string c)=>                
        {
            if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
                sLevel[y * nLevelWidth + x] = c;
        };
    }
}

That´s a good article about delegates.

Wordy phrase replacements, Leave out. as a result of, because, because of, since attached herewith is, here are, here's, with this (or leave out) effect many changes, change it would appear that, apparently, clearly, obviously, plainly, seemingly. If you replace "that" with "because", you change the syntax; it becomes a preposition phrase function instead of a clause and its function becomes an adjunct of reason explaining why they were furious. Btw, it can't be object since "furious" is not a verb. – BillJ Dec 22 '16 at 16:43


Replace Your Green Card, The following sections will help you learn more about replacing your Green Card. When to Replace Your Green C. How can I replace string foo with bar in all files in the current directory? do the same recursively for sub Stack Exchange Network Stack Exchange network consists of 176 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.


Lambda Expressions doesn't have a compile time type, but is convertible to any matching delegate or expression type. That's why according to MSDN lambda expressions can't be implicitly typed (also check this answer by Eric Lippert

So:

Func<int, int, string> GetTile = (int x, int y) =>
{
    if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
        return sLevel[y * nLevelWidth + x];
    else
        return "";
};

Action<int, int, string> SetTile = (int x, int y, string c)=>                
{
   if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
       sLevel[y * nLevelWidth + x] = c;
};

11 Non-Alcoholic Substitutes for Wine (Both Red and White), This article presents 11 substitutes for when you don't have wine on hand or if Summary Wine vinegar can replace wine in cooking without having a major Most Diets Don't Work for Weight Loss After a Year: Here's Why. Sometimes, verbs of being are necessary. But a lot of the time, “is,” “are,” “was” and “were” are B-O-R-I-N-G, boring. We challenge you to run through your story and highlight every single verb of being. Then, think about which ones you can replace—try to replace as many as you can. Here are 30 fabulous verbs to get you started.


You can declare two delegates like below:

delegate void ActionRef(ref int x, ref int y, string c);
delegate string FuncRef(ref int x, ref int y);

And instead of using var, you can these delegates as the type of function.

public GameLogic()
{
    FuncRef GetTile = (ref int x, ref int y) =>
    {
        if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
            return sLevel[y * nLevelWidth + x];
        else
            return "";
    };

    ActionRef SetTile = (ref int x, ref int y, string c) =>
    {
        if (x >= 0 && x < nLevelWidth && y >= 0 && y < nLevelHeight)
            sLevel[y * nLevelWidth + x] = c;
    };
}

Replacing your Passport After the Storm, For COVID-19 Travel Information click here To replace a lost passport, you will need to apply in person and include the following: Form DS-64 (Statement� There’s no paste here, though, so it won’t do the trick when texture is what your recipe needs. But in terms of flavor, sesame oil is a pinch-hitter. But since this substitute is oiler than tahini, you’ll likely need less of it—start with half the amount and adjust to taste.


U.S. Passport Fees, Facilities or Problems, Here's how you know. Dot gov. Official websites Locate a local passport acceptance facility - This can be a post office, some public libraries or clerk of court office. Expedited processing is not available when applying from outside the U.S. Find information on how to replace your lost or stolen passport. You can call it up if you want to buy new songs and albums, but if you do subscribe to Apple Music, you likely won't have much use for a store. Apple TV will have an app for the Mac, iPhone and


9 synonyms of replace from the Merriam-Webster Thesaurus, plus 3 related words, definitions, and antonyms. Find another word for replace. Replace: to take the place of.


Hmm, from my observation ' substitute ' is used on persons whereas ' replace ' is on things. Actually there are no rules governing this I believe, it is just that people tend to use it that way. with = involve the person and his action by = just only as a means to achieve one's objective or motive. eg. I settle the bill with/by cash.