Skip to content

Instantly share code, notes, and snippets.

@RetiredQQ
Created June 4, 2017 14:52
Show Gist options
  • Save RetiredQQ/bbe1ffea5fee87d76af3d9bce8a41565 to your computer and use it in GitHub Desktop.
Save RetiredQQ/bbe1ffea5fee87d76af3d9bce8a41565 to your computer and use it in GitHub Desktop.
Initialize and delete array elements.
public static class Arrays {
public static T[] InitializeWithDefaultInstances < T > (int length) where T: new() {
T[] array = new T[length];
for (int i = 0; i < length; i++) {
array[i] = new T();
}
return array;
}
public static void DeleteArray < T > (T[] array) where T: System.IDisposable {
foreach(T element in array) {
if (element != null)
element.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment