get values in Arraylist using c#

Posted by Arjunan88 under C# on 9/22/2012 | Points: 10 | Views : 45406 | Status : [Member] | Replies : 5
How to get values in arraylist from users at run time?Explain with simple example




Responses

Posted by: Vikash on: 9/22/2012 [Member] Starter | Points: 25

Up
0
Down
Please You can use generic List<Type> instead of Arraylist

List<string> mylist = new List<string>();
Console.WriteLine("Enter Name");
string name = Console.ReadLine();
mylist.Add(name);



Plz mark as answer if it is helpful.. As it helps other to go through....

Regards,
Vikash Pathak

Arjunan88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/24/2012 [Member] Bronze | Points: 25

Up
0
Down
1)An arraylist stores objects so in order to retrieve them you need to cast them to the appropiate type.
2)ArrayList resizes dynamically.
3)ArrayList as return value.


Example:-
using System.Collections;
class Program
{
static void Main()
{
//
// Create an ArrayList and add three elements.
//
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
}
}

Arjunan88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/24/2012 [Member] Bronze | Points: 25

Up
0
Down
Please mark as answer if it helpful to you..

Arjunan88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pgayath on: 10/12/2012 [Member] Starter | Points: 25

Up
0
Down
Arjun:

You can use the below code to add and display the value from ArrayList.

static void Main(string[] args)

{
ArrayList arrList = new ArrayList();

Console.WriteLine("Enter Value");
string name = Console.ReadLine();
arrList.Add(name);

Console.WriteLine("The value in the array list is");
Console.WriteLine(arrList[0].ToString());
Console.ReadLine();

}


Thanks and Regards,
Gayathri P

Arjunan88, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Perumal93 on: 10/27/2012 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; // You have to use this namespace for ArrayList class.

namespace ArrayList_Demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("ArrayList values are : ");

ArrayList arrList = new ArrayList();

arrList.Add(1);

arrList.Add(2);

arrList.Add(3);

arrList.Add(99.9);

arrList.Add("One");

arrList.Add("Two");

arrList.Add("Three");

foreach (object values in arrList)
{
Console.WriteLine(values);
}
}
}
}


Arjunan88, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response