0

Is it possible to set all int array values to 0 with Lambda/Linq?

public int[] Reset()
{
    int[] M = new int[MIlg]; \\MIlg - length of array
    for (int i = 0; i < M.Length; i++)
    {
        M[i] = 0;
    }
    return M;
}
3
  • 1
    LINQ is for querying, not modifying. Commented May 22, 2016 at 7:36
  • As an aside, I would strongly advise you to use more meaningful names - MIlg is a very obscure name, IMO. Commented May 22, 2016 at 7:39
  • In c#, new array of int will always create continuous block of int with 0 value. Commented May 22, 2016 at 7:39

1 Answer 1

4

LINQ is inherently about querying, not modifying. But there's already a method that does this - Array.Clear:

Array.Clear(array, 0, array.Length);

That's assuming you want to clear an existing array, of course. Your current method doesn't make much sense, as a new array already starts with every element set to the default, so you can just use new int[length]. I'd expect a method called Reset to affect an existing array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.