dynamic memory allocation

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
Is there a dynamic memory allocation class in C#?

I have in mind something equivalent to C++ push_back?



The reason is that I am reading an entire column from a database table. I do not know the column length in advance.

It is something that can vary.

I would like to get all the items, I read from a specific column of a database table, placed in a list.

Unluckily the library that C# uses to access PostgreSQL, namely Npgsql, reads one row at a time.

So to get an entire column, whose length is unknown, we have to keep reading until the column end is reached.

Something similar to read a file until EOF is encountered.

May I add consecutively read items to a list in C#?

What is the best strategy in this case?

Thank you in advance

Mauede
 

Attachments

  • Screenshot 2021-12-05 at 13.29.26.png
    Screenshot 2021-12-05 at 13.29.26.png
    173.2 KB · Views: 15
In general, each time you call new in C# you are dynamically allocating memory. When you call Add() on List, the framework will allocate more memory dynamically to be able to store that new value or a reference into an array that the List maintains under the cover.
 
In the future, please post code in code tags, not as screenshots.
 
Unluckily the library that C# uses to access PostgreSQL, namely Npgsql, reads one row at a time.
It has a data adapter class, NpgsqlDataAdapter, that fills a DataTable/DataSet with all results.
 
Also, if your objective to get data from a single column into a list, using the SQL data adapter is the expensive way to get it. Using the SQL data reader is the cheaper more efficient way. ns pseudo code:
C#:
var list = new List<some type>();
using (var reader = command.ExecuteQuery())
{
    while (reader.Next())
        list.Add(reader.GetSomeType(columnNumber))
}
 
Back
Top Bottom