Once upon a time, I had this in C++:
template <typename T>
class Array2D
{
// ...
T& get(const int x, const int y)
{
return m_array[y * m_width + x];
}
}
I was using a 2D array to have a type of DLinkedList. Each cell in a map would have a linked list.
Well, now I'm trying to covert that into C#.
public T Get(int x, int y)
{
return m_array[y * m_width + x];
}
The problem is it's not returning a reference as previous done in C++, but rather a value.
So, I can't do something like this...
map = new Array2D<List<int>>(10, 10);
for (int y = 0; y < map.Height; y++)
{
for (int x = 0; x < map.Width; x++)
{
map.Get(x, y).Add(0);
}
}
Even if I use the Set() method, adding 0 to the end of the list is trying to set an integer value of type List. So that doesn't work.
I tried using unsafe code with pointers, but the compiler yelled at me for trying it on a managed type of T.
Overloading [,] is not available.
I feel I'm missing something very obvious to do, but I can't grasp it. Any thoughts? Thank you.