472,353 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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

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<classvClass;

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

C#
List<classlstClass;

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

GhostInAK wrote:
Hello Je*********@gmail.com,

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

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

Jeff
ad***@binindex.net wrote:
You can use an arraylist. Is that what you are talking about.
Jeff.Boe...@gmail.com 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
Sep 27 '06 #1
6 13809
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]
- mv*@spam.guard.caspershouse.com

<Je*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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<classvClass;

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

C#
List<classlstClass;

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

GhostInAK wrote:
>Hello Je*********@gmail.com,

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

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

Jeff
ad***@binindex.net wrote:
You can use an arraylist. Is that what you are talking about.
Jeff.Boe...@gmail.com 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

Sep 27 '06 #2
Hello Je*********@gmail.com,

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<classvClass;
vClass.resize(1000);
vClass[37].Property = 4;
C#
List<classlstClass;
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

GhostInAK wrote:
>Hello Je*********@gmail.com,

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

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

Jeff
ad***@binindex.net wrote:
You can use an arraylist. Is that what you are talking about.
Jeff.Boe...@gmail.com 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
>


Sep 27 '06 #3
Interesting idea, thanks.

Jeff
GhostInAK wrote:
Hello Je*********@gmail.com,

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<classvClass;
vClass.resize(1000);
vClass[37].Property = 4;
C#
List<classlstClass;
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

GhostInAK wrote:
Hello Je*********@gmail.com,

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

-Boo

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

Jeff
ad***@binindex.net wrote:
You can use an arraylist. Is that what you are talking about.
Jeff.Boe...@gmail.com 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
Sep 28 '06 #4
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

Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mv*@spam.guard.caspershouse.com

<Je*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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<classvClass;

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

C#
List<classlstClass;

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

GhostInAK wrote:
Hello Je*********@gmail.com,

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

-Boo

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

Jeff
ad***@binindex.net wrote:
You can use an arraylist. Is that what you are talking about.
Jeff.Boe...@gmail.com 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
Sep 28 '06 #5
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.

On 27 Sep 2006 18:46:46 -0700, Je*********@gmail.com wrote:
>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
--
http://www.kynosarges.de
Sep 28 '06 #6
"Chris Nahr" <ch************@kynosarges.dewrote in message
news:l6********************************@4ax.com...
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
Sep 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons...
5
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() {...
3
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface -...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm)...
4
by: Peted | last post by:
I have the following code public enum pdfFlags { PFD_DRAW_TO_WINDOW, PFD_DRAW_TO_BITMAP, PFD_SUPPORT_GDI, PFD_SUPPORT_OPENGL,...
0
by: DR | last post by:
System.Collections.Generic.List<intmyList = new System.Collections.Generic.List<int>(100); Does this preallocate 100 integers? Also, is there...
2
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty...
6
by: Peter | last post by:
I have a WebService which returns a List of RunningReport class How do I read this XML data on the client side. How do I convert...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.