473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

question about the initialization of an array of generic queue

Here are the codes

Queue<string>[] q = new Queue<string>[2];

Console.WriteLine(q[0].Count + "");

The program will crash since q[0] is null.

So my question is that what is the best way for new to initialize the array
with generic queue as the value?

I can do for statement and then do q[i] = new Queue<string>();

But,is there a better way to do it?

Thanks

Chris
Oct 15 '08 #1
3 2964
But,is there a better way to do it?

No, not really. Array values initialize to their default values: 0 /
false / null / etc. Assuming you want each item in the array to be a
different queue, you will need to loop and create (new) a different
object for each.

Marc
Oct 15 '08 #2
chrisben wrote:
Here are the codes

Queue<string>[] q = new Queue<string>[2];

Console.WriteLine(q[0].Count + "");

The program will crash since q[0] is null.

So my question is that what is the best way for new to initialize the
array with generic queue as the value?

I can do for statement and then do q[i] = new Queue<string>();

But,is there a better way to do it?
Well, you can always write a reusable function for that:

static class ArrayHelper
{
static void FillUsingDefaultConstructor<T(this T[] array) where T :
new
{
for( int i = 0; i < array.Length; i++ )
array[i] = new T();
}
}

Queue<string>[] q = new Queue<string>[2];
q.FillUsingDefaultConstructor();
>
Thanks

Chris

Oct 15 '08 #3
Why not use List<Queue<string>?

On 10/15/2008 11:44 AM, chrisben wrote:
Here are the codes

Queue<string>[] q = new Queue<string>[2];

Console.WriteLine(q[0].Count + "");

The program will crash since q[0] is null.

So my question is that what is the best way for new to initialize the array
with generic queue as the value?

I can do for statement and then do q[i] = new Queue<string>();

But,is there a better way to do it?

Thanks

Chris

--
Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net
icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

.... "I live for immortality."--Laura Thurston
Oct 16 '08 #4

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

Similar topics

0
by: Dave Coate | last post by:
I am working on a generic way to launch multiple similar processes (threads) at once, but limit the number of threads running at any one time to a number I set. As I understand it the following...
10
by: INeedADip | last post by:
I am trying to use a generic (reflection) IComparer class to sort a generic list but I get the error: Unable to cast object of type 'GenericComparer' to type 'System.Collections.Generic.Icomparer...
1
by: INeedADip | last post by:
I am trying to use the following generic (reflection) class as the ICamparer parameter for a generic list..but I get the error: "Unable to cast object of type 'GenericComparer' to type...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
4
by: nhmark64 | last post by:
Hi, Does System.Collections.Generic.Queue not have a Synchronized method because it is already in effect synchronized, or is the Synchronized functionality missing from...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
1
by: A. Burch | last post by:
What is wrong with this type of generic... I get this error.... Error 1 Inconsistent accessibility: parameter type 'System.Collections.Generic.Queue<TMQ.TheRec>' is less accessible than...
1
by: Back 9 | last post by:
Hi all, I would like to make a generic Queue arrry in C# like below. Queue<int> queue = new Queue<int> (); But the compiler keep complaining about the part and honestly I don't know how to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.