Search code examples
c#multidimensional-arrayoperation

Can't do arithmetical operations on array values in c#


I'm writing a program for homework, but I have stumbled upon a very hard problem for me. Now, I'm pretty new to C#, so please bear with me. This may be really easy and obvious. On-topic: C# doesn't allow me to perform arithmetical operations on multidimensional array values:

if(map[0,1] - map[0,0] == 10)

This statement doesn't return a value, but instead throws me an error:

Object reference not set to an instance of an object.


Solution

  • You need to first declare the array. Example:

    var map = new int[2,2];
    

    creates a two-dimensional array with four integer elements.