473,703 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<classvCl ass;

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

C#
List<classlstCl ass;

lstClass = new List<class>(100 0);
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*********@gma il.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...@gma il.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 13912
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.co m

<Je*********@gm ail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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<classvCl ass;

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

C#
List<classlstCl ass;

lstClass = new List<class>(100 0);
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*********@gma il.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...@gm ail.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*********@gma il.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<classvCl ass;
vClass.resize(1 000);
vClass[37].Property = 4;
C#
List<classlstCl ass;
lstClass = new List<class>(100 0);
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*********@gma il.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*********@gma il.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<classvCl ass;
vClass.resize(1 000);
vClass[37].Property = 4;
C#
List<classlstCl ass;
lstClass = new List<class>(100 0);
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*********@gma il.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...@g mail.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.co m

<Je*********@gm ail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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<classvCl ass;

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

C#
List<classlstCl ass;

lstClass = new List<class>(100 0);
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*********@gma il.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...@gma il.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*********@gma il.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.dew rote in message
news:l6******** *************** *********@4ax.c om...
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
15177
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 returns true/false if target string exists in the generic list...is not clear to me. Any suggestions? System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(); foreach(string s in myAnotherArray)
4
52981
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 needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
5
1921
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() { pageSize = (this.Count == 0) ? 1 : this.Count; pageNumber = 1; }
3
2095
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 - TestClass : IPerson I put this class in a List<TestClass> = new List<TestClass>(); then I pass this List<TestClass> to a function which takes an argument List<IPerson> person
4
2954
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) Now, when I compile it, I get "warning C4172: returning address of local variable or temporary". In good old 'C', that would indicate that a 'static' was missing from the declaration of the returned value.
4
10610
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, PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
0
1333
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 any way to preallocate an array of objects all at once? MyOb al= new MyOb; for (int z = 0; z < nCount; z++)
2
6155
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 well, but I am having trouble with generic lists, like List<char>. What I have working is : Type type = newObj.GetType(); //newObj is what I'm trying to dump
6
2982
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 List<RunningReportfrom the WebService side to List<RunningReporton the client side I have tried the following: List<RunningReportreportList = null; localhost.ReportService localrs = new localhost.ReportService();
0
8759
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8669
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8963
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4433
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.