2 Replies - 5847 Views - Last Post: 23 April 2010 - 08:09 PM Rate Topic: -----

#1 complete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 12-April 07

Declaring an array of linked list in C#

Posted 23 April 2010 - 07:29 PM

Declaring an array of linked list in C#

How do I declare an array of some class in C#? I wonder if it is even allowed or possible.

I got the compile error message "Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)" when I tried to declare an array of linked lists.

public LinkedList<LevelNode>[2] ExistingXMLList;

Also, if I wanted to create a small array of strings, isn't this the way:

string [2] inputdata;

Is This A Good Question/Topic? 0
  • +

Replies To: Declaring an array of linked list in C#

#2 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Declaring an array of linked list in C#

Posted 23 April 2010 - 08:04 PM

on a list/linked list collection you don't need to specify the size beforehand.

LinkedList<LevelNode> ExistingXMLList= new LinkedList<LevelNode>();



And for an array...

string[] inputdata = new string[2];


Was This Post Helpful? 0
  • +
  • -

#3 remorseless   User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: Declaring an array of linked list in C#

Posted 23 April 2010 - 08:09 PM

Actually, in C#, when declaring an array it's not like C++. This is how to declare a string array in C#:
string[] inputdata = new string[2];



So, for an array of LinkedLists:
public LinkedList<LevelNode>[] ExistingXMLList = new LinkedList<LevelNode>[2];



Another way you could do it, even though it isn't widely used, is:
public LinkedList<LevelNode>[] ExistingXMLList = { null, null };



Hope I helped! )
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1