Created
September 30, 2021 14:03
-
-
Save sapphire-al2o3/2d9eb8593edbeac408fc89099f9f4496 to your computer and use it in GitHub Desktop.
Listをデフォルト値で初期化する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
List<int> list = new List<int>(new DefaultInitCollection(10)); | |
foreach (var e in list) | |
{ | |
Console.WriteLine(e); | |
} | |
class DefaultInitCollection : ICollection<int> | |
{ | |
int _size = 0; | |
public DefaultInitCollection(int size) => _size = size; | |
public int Count => _size; | |
public void CopyTo(int[] array, int arrayIndex) | |
{ | |
// 何もしない | |
} | |
public bool IsReadOnly => true; | |
public void Add(int item) {} | |
public void Clear() {} | |
public bool Contains(int item) => false; | |
public IEnumerator<int> GetEnumerator() => null; | |
public bool Remove(int item) => false; | |
IEnumerator IEnumerable.GetEnumerator() => null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment