question about the initialization of an array of generic queue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Y2hyaXNiZW4=?=

    #1

    question about the initialization of an array of generic queue

    Here are the codes

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

    Console.WriteLi ne(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
  • Marc Gravell

    #2
    Re: question about the initialization of an array of generic queue

    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

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: question about the initialization of an array of generic queue

      chrisben wrote:
      Here are the codes
      >
      Queue<string>[] q = new Queue<string>[2];
      >
      Console.WriteLi ne(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 FillUsingDefaul tConstructor<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.FillUsingDefa ultConstructor( );
      >
      Thanks
      >
      Chris

      Comment

      • Michael J. Ryan

        #4
        Re: question about the initialization of an array of generic queue

        Why not use List<Queue<stri ng>?

        On 10/15/2008 11:44 AM, chrisben wrote:
        Here are the codes
        >
        Queue<string>[] q = new Queue<string>[2];
        >
        Console.WriteLi ne(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)the roughnecks(dot) net - www.theroughnecks.net
        icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

        .... "I live for immortality."--Laura Thurston

        Comment

        Working...