Array List
-
Hello Everyone, ArrayList arr = new ArrayList(); arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); Arraylist is hetergenous so i've taken three datatype values as shown above But anybody let me know how to print these object's values........
Hi, There is no problem for printing your ArrayyList values by following. ArrayList arr = new ArrayList(); arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); for (int i = 0; i < arr.Count; i++) lblResult.Text += arr[i];
Naresh Patel
-
Hello Everyone, ArrayList arr = new ArrayList(); arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); Arraylist is hetergenous so i've taken three datatype values as shown above But anybody let me know how to print these object's values........
you can use ToString for primitive types like int ,double,string,... but in some cases ToString does not return the data we expect (like classes we define by our self) so in those cases you can override the ToString() method.
foreach(object obj in arr)
Console.WriteLine(obj.ToString());hope the post would be useful
-
Hello Everyone, ArrayList arr = new ArrayList(); arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); Arraylist is hetergenous so i've taken three datatype values as shown above But anybody let me know how to print these object's values........
In addition to the
ToString()
method suggested above, you can also useGetType()
on any object which returns an object of typeType
. This object has all the information you could ever need on what types are inside your arraylist. -
Hello Everyone, ArrayList arr = new ArrayList(); arr.Add("Manoj"); arr.Add(4); arr.Add(4.5); Arraylist is hetergenous so i've taken three datatype values as shown above But anybody let me know how to print these object's values........
I think you should use an object then add it to your arraylist. such as You can create an object: myObject obj =new myObject(); obj.Title="Manoj"; obj.Age=4; obj.Weight=4.5; //Add to arraylist ArrayList arr = new ArrayList(); arr.Add(obj); //Get from arraylist for(int i = 0; i < arr.Count; i++) { myObject obj = arr[i] as myObject; Console.Write(obj.Title); .... } Hope that help you vtkiet05