Help us understand the problem. What is going on with this article?

# C# における配列

その配列、無駄にリソースを使っていませんか？

こんな時や...

```int[] hoge = { 1, 2, 3 };

//Aさん: 拡張したいなあ...

var newHoge = new int[6];
newHoge[0] = hoge[0];
newHoge[1] = hoge[1];
newHoge[2] = hoge[2];
newHoge[3] = 3;
newHoge[4] = 4;
newHoge[5] = 6;

//Aさん: これでよし。

------------------------------------------

//Bさん: いや、おらはもうちょっと賢いぞ。
Array.Copy(hoge, newHoge, 3);
newHoge[3] = 3;
newHoge[4] = 4;
newHoge[5] = 6;
```

こんな時。

```//配列の特定の中身を置き換えたい。
int[] hoge = { 1, 2, 3, 4, 5 };

for(int n = 0; n < hoge.Length; n++)
{
if(hoge[n] == 3)
{
hoge[n] = 999;
}
}
```

いちいち書いてられません。
そんな時に「あったら便利」なクラスを作ってみました。

# 使用例

## ToArrayString

```int[] hoge = { 1, 2, 3 };
Console.WriteLine(ArrayEx.ToArrayString(hoge));
//Output: (3) [ 1, 2, 3 ]
```

## SelfAppend

```int[] hoge = { 1, 2, 3 };
ArrayEx.SelfAppend(ref hoge, 3);
//Output: (4) [ 1, 2, 3, 3 ]
```

## SelfExtend

```int[] hoge = { 1, 2, 3 };
ArrayEx.SelfExtend(ref hoge, 10);
//Output: (10) [ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
```

## SelfConcat

```int[] hoge = { 1, 2, 3 };
int[] geho = { 4, 5, 6 };
ArrayEx.SelfConcat(ref hoge, geho);
//Output: (6) [ 1, 2, 3, 4, 5, 6 ]
```

## SelfPrepend

```int[] hoge = { 1, 2, 3 };
ArrayEx.SelfPrepend(ref hoge, 999);
//Output: (4) [ 999, 1, 2, 3 ]
```

## SelfReverse

```int[] hoge = { 1, 2, 3 };
ArrayEx.SelfReverse(ref hoge);
//Output: (3) [ 3, 2, 1 ]
```

## SelfReplace

```int[] hoge = { 1, 2, 3 };
ArrayEx.SelfReplace(ref hoge, 3, 999);
//Output: (3) [ 1, 2, 999 ]
```

## SelfReplaceByIndex

```int[] hoge = { 1, 2, 3 };
ArrayEx.SelfReplaceByIndex(ref hoge, 999, 0);
//Output: (3) [ 999, 2, 3 ]
```
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away