Home c# Dynamic array of C # objects

Dynamic array of C # objects




There is a class (public class MyNum). How to declare and then initialize a two-dimensional dynamic array with objects of my class?

No matter how I try, errors. How do I:

public MyNum [][] Array1= new MyNum[2][];

Next, I try like this:

Array1[0][0]=new MyNum(arg1);

Naturally I get crit, because there is no memory allocated for the array element Array[0][0]. It would seem that Everything should facilitate Array1[0].Append()

But that doesn’t work either.

How to use dynamic arrays?

Additional question
What is the difference between ads:

public MyNum[,] Array1; 
public MyNum[][] Array1; 

I do not understand and it is not written about it anywhere. As usual !!!

Answer 1, authority 100%

As far as I understand, a dynamic array means exactly the one that can be changed during the program’s work. Add items, remove items.
In Sharp, such an “array” is List. (judging by the question, you switched from python, that’s why I’m putting it this way)

List is a wrapper around Array. In Java and some other languages, this is called an ArrayList, which is more in line with what it really is. If you want, you can google exactly how it works.

What exactly you need is to create a sheet of sheets.

var list = List<List<MyNum>>();

then you can use it however you want.

list.add( new List<MyNum>() ); // add the line
list[0].add( new MyNum(2) );   // add a cell to a row

and refer to the desired cell after that

var myNum = list[0][0];

The use of arrays for this case is undesirable because the append () function is in fact a re-creation of an array in a new memory area with a size of +1 cell, followed by copying the data. So this operation is quite expensive in terms of resources and should be avoided.

Specifically, arrays should be used only if you know in advance exactly how many elements will be written and their number will not change.

If you want to use an array, then you need to do the same approach that I did with the sheet. First, append a “row for cells”, and only then append a cell into a row.

Answer 2

When this code is executed

public MyNum [][] Array1= new MyNum[2][];

Array1will have two nullelements. This is why an error occurs when trying to access Array1[0][0].

For this to work, before referring to a specific element, you need to create this array, for example:

Array1[0] = new MyNum[2]();

Then the line Array1[0][0]=new MyNum(arg1);will no longer give an error.

You can also immediately initialize the array, for example:

new MyNum[2][] { new[] { new MyNum() }, new[] { new MyNum() } };

If the quantity is not known in advance, or may change during execution, you can use List<T>. It contains the Addoperation to add new elements.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions