Generic C# List<> resize vs. Template C++ vector resize

  • Thread starter Thread starter Jeff.Boeker
  • Start date Start date
J

Jeff.Boeker

I'm learning a lesson in how I need to be more specific :)

In C++ I can resize a vector and it will allocate memory and it will
call the default constructor if necessary (or I can supply an instance
for the copy constructor).

For example:

C++
vector<class> vClass;

vClass.resize(1000);
vClass[37].Property = 4;

C#
List<class> lstClass;

lstClass = new List<class>(1000);
for (int instance = 0; instance != 1000; ++instance)
vClass.Add(new class());
vClass[37].Property = 4;

I'm hoping there is a better way then my loop.

Jeff
 
Jeff,

Nope. Short of extending list to take a delegate which is called for
the construction of new instances, there isn't much you can do.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm learning a lesson in how I need to be more specific :)

In C++ I can resize a vector and it will allocate memory and it will
call the default constructor if necessary (or I can supply an instance
for the copy constructor).

For example:

C++
vector<class> vClass;

vClass.resize(1000);
vClass[37].Property = 4;

C#
List<class> lstClass;

lstClass = new List<class>(1000);
for (int instance = 0; instance != 1000; ++instance)
vClass.Add(new class());
vClass[37].Property = 4;

I'm hoping there is a better way then my loop.

Jeff
Hello (e-mail address removed),

Check out .Add(), .Remove(), and .Capacity

-Boo
 
Hello (e-mail address removed),

My mistake.
You could create an array of objects.. SomeClass tArray[1000]
Then call the array's .Initialize method. Then pass the array to the generic's
..AddRange method.

YUCK.

-Boo
I'm learning a lesson in how I need to be more specific :)

In C++ I can resize a vector and it will allocate memory and it will
call the default constructor if necessary (or I can supply an instance
for the copy constructor).

For example:

C++
vector<class> vClass;
vClass.resize(1000);
vClass[37].Property = 4;
C#
List<class> lstClass;
lstClass = new List<class>(1000);
for (int instance = 0; instance != 1000; ++instance)
vClass.Add(new class());
vClass[37].Property = 4;
I'm hoping there is a better way then my loop.

Jeff
Hello (e-mail address removed),

Check out .Add(), .Remove(), and .Capacity

-Boo
 
Interesting idea, thanks.

Jeff said:
Hello (e-mail address removed),

My mistake.
You could create an array of objects.. SomeClass tArray[1000]
Then call the array's .Initialize method. Then pass the array to the generic's
.AddRange method.

YUCK.

-Boo
I'm learning a lesson in how I need to be more specific :)

In C++ I can resize a vector and it will allocate memory and it will
call the default constructor if necessary (or I can supply an instance
for the copy constructor).

For example:

C++
vector<class> vClass;
vClass.resize(1000);
vClass[37].Property = 4;
C#
List<class> lstClass;
lstClass = new List<class>(1000);
for (int instance = 0; instance != 1000; ++instance)
vClass.Add(new class());
vClass[37].Property = 4;
I'm hoping there is a better way then my loop.

Jeff
Hello (e-mail address removed),

Check out .Add(), .Remove(), and .Capacity

-Boo

Thanks, but I meant the generic List which is part of ,Net 2.0.

Jeff
(e-mail address removed) wrote:
You can use an arraylist. Is that what you are talking about.
(e-mail address removed) wrote:

Hello,

It looks like there is no List<> resize in C#, similar to C++'s
STL vector<>. Am I missing some alternative?

Thanks,

jeff
 
Hello Nicholas,

Thanks for confirming that. I'm curious why such a function wasn't
build in. I'm sure the designer of the List class looked at C++'s
vector and made a conscious decision to omit it.

Jeff
Jeff,

Nope. Short of extending list to take a delegate which is called for
the construction of new instances, there isn't much you can do.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm learning a lesson in how I need to be more specific :)

In C++ I can resize a vector and it will allocate memory and it will
call the default constructor if necessary (or I can supply an instance
for the copy constructor).

For example:

C++
vector<class> vClass;

vClass.resize(1000);
vClass[37].Property = 4;

C#
List<class> lstClass;

lstClass = new List<class>(1000);
for (int instance = 0; instance != 1000; ++instance)
vClass.Add(new class());
vClass[37].Property = 4;

I'm hoping there is a better way then my loop.

Jeff
Hello (e-mail address removed),

Check out .Add(), .Remove(), and .Capacity

-Boo

Thanks, but I meant the generic List which is part of ,Net 2.0.

Jeff
(e-mail address removed) wrote:
You can use an arraylist. Is that what you are talking about.
(e-mail address removed) wrote:

Hello,

It looks like there is no List<> resize in C#, similar to C++'s STL
vector<>. Am I missing some alternative?

Thanks,

jeff
 
Runtime performance and simplicity I'd say. The idea is that memory
only needs to be zeroed when the backing array is extended or elements
are recycled. If the List class did call a default constructor for
each element someone could write a very slow constructor that would
effectively hang the program at this point.
 
Chris Nahr said:
Runtime performance and simplicity I'd say. The idea is that memory
only needs to be zeroed when the backing array is extended or elements
are recycled. If the List class did call a default constructor for
each element someone could write a very slow constructor that would
effectively hang the program at this point.

Actually, I think it's an oversight or a deliberate simplification.

The C++ std::vector has exactly the same issues, the designers of that
library simply decided that it was better to expose the functionality and
let the programmer decide when/if to use it, rather than assuming that the
designer always knows best.

-cd
 
Back
Top