CSharp 实现动态数组 源代码
时间: 2023-10-22 13:20:47 浏览: 46
Sure, I can help you with that. Here's a sample C# code for implementing a dynamic array:
```
using System;
class DynamicArray<T>
{
private T[] array;
private int size;
public DynamicArray(int capacity = 16)
{
array = new T[capacity];
size = 0;
}
public void Add(T item)
{
if (size == array.Length)
{
Array.Resize(ref array, array.Length * 2);
}
array[size++] = item;
}
public T Get(int index)
{
if (index < 0 || index >= size)
{
throw new IndexOutOfRangeException();
}
return array[index];
}
public int Size
{
get { return size; }
}
}
```
Hope that helps! Do you have any other questions?