Jump to content

Dynamic arrays in C#?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Swarvy

Swarvy

    Newbie

  • Members
  • Pip
  • 3 posts
Hello All, I am new to C# although I am familiar with C, C++ and python, anyway, I was wondering if there was any support available for dynamic arrays in C#. I googled it before and I came across someone saying you can do it using Lists as shown below:

List<uint> possible_numbers = new List<uint>();


Although when I try to compile this code, I get an error that 'List' is undefined. Perhaps I am not including the correct 'using' statements at the start of my code.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Are you using the List in System.Collections.Generic?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
What are you using for development? Visual Studio? If so, what version.

#4
kernelcoder

kernelcoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 288 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal
The 'List' is a generic type collection and all the generic type collections are in System.Collections.Generic namespace. So put a line to using it at the above of your code or type the full name for the code like follows...
System.Collections.Generic.List<uint> possible_numbers = new System.Collections.Generic.List<uint>();


#5
AceInfinity

AceInfinity

    Advanced Member

  • Members
  • PipPipPip
  • 35 posts
using System.Collections.Generic;

You haven't defined the namespace for which List comes from. Also List(of Type) is not an array, similarly it stores a collection of data, but it's not an array.

Microsoft MVP (2012) - My MSDN Profile
~ "The universe is an intelligence test." - Timothy Leary ~


#6
sweta

sweta

    Newbie

  • Members
  • Pip
  • 3 posts
Hi, As per my observation ,most of the time, we need to have arrays that we don't know the values or how many items its coming. The example shown is an example of a completely dynamic array. The only information that has been set at design time is the data type (int), the variable name (intArray), and that it is an array ([]). The values and the number of values will be based on user input or data retrieved from at runtime. The following mentioned are some examples of how this type of array is declared.
int[]intArray;
Fixed length at the time of declaration but not the values
intArray = new int[5];
Practical Example of a Dynamic Array
In the example below, I used one textbox,listbox,richtextbox,and two buttons.
1)When the user clicks the “add button” with the help of textbox user can add the values in the richtextbox.

2) When the user clicks the "Create Array" button, the array size is set to the number of items in the list box and a for loop is used to add the values to the dynamic array.
C# Windows Application Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void btnADD_Click(object sender, EventArgs e)
		{
			lstBoxValues.Items.Add(textBox1.Text);
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			string[] strListItems;
			//the number of items in the list box is the size of our array.
			int intNumItems = lstBoxValues.Items.Count;//get the number of items
			strListItems = new string[intNumItems];

			//use a for iteration to add the items.
			//List boxes are also 0 based. The first item in the list will be referred to as lstBoxValues[0].
			for (int intX = 0; intX < lstBoxValues.Items.Count; intX++)
				strListItems[intX] = lstBoxValues.Items[intX].ToString();

			//Show array properties in a rich text box named rtbArrayValues
			rtbArrayValues.Text = "Total number of elements = " + strListItems.LongLength + "\n";//the \n is a return

			//show all of the values using the for iteration
			for (int intX = 0; intX <= strListItems.GetUpperBound(0); intX++)
				rtbArrayValues.Text += "value " + intX + " is " + strListItems[intX] + "\n";
		}
		}
	}
Sweta




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users