Skip to content

Instantly share code, notes, and snippets.

@sapphire-al2o3
Created September 30, 2021 14:03
Show Gist options
  • Save sapphire-al2o3/2d9eb8593edbeac408fc89099f9f4496 to your computer and use it in GitHub Desktop.
Save sapphire-al2o3/2d9eb8593edbeac408fc89099f9f4496 to your computer and use it in GitHub Desktop.
Listをデフォルト値で初期化する
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