简体   繁体   中英

How do I pass a new array as a parameter in C#

I am trying to use the following constructor:

public Tile(int top, int right, int bottom, int left, int _prefabIndex, bool _isDouble, int _villageIntake, bool _isControl, bool _isBuilder, bool[] _hasWater, int _homeVillage, int _isResevoir)

And I'm trying to use it in only one line of code for simplicity. I have tried using:

newTile = new Tile(1, 3, 1, 3, 12, false, -1, true, false, {false, true, false, false}, -1, -1);

But I get a compile error on the Boolean array parameter. I know I could use:

bool[] boolArray = {false, true, false, false};
newTile = new Tile(1, 3, 1, 3, 12, false, -1, true, false, boolArray, -1, -1);

But I am wondering if there is a correct syntax for passing a new array as a parameter?

You need to do something like this:

new[]{false, true, false, false}

So it will be:

Tile(1, 3, 1, 3, 12, false, -1, true, false, new[]{ false, true, false, false}, -1, -1);

For:

private void Function(bool[] switches) ...

Do:

Function(new []{ true, false });

However, it's also useful to note that if you can make the array the last parameter, you can use the "params" keyword:

private void ParamsFunction(params bool[] switches) ...

...and do this:

ParamsFunction(true, false);

You can do like this:

public bool SetPriority(new []{high, medium, low})
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM