var arr = new[] {new Point(10, 20)};
var lst = new List<Point> {new Point(30, 40)};
Console.WriteLine(arr[0].ToString());
Console.WriteLine(lst[0].ToString());
arr[0].X = 50;
arr[0].Y = 60;
Console.WriteLine(arr[0].ToString());
//lst[0].X = 70;
//lst[0].Y = 80;
//Console.WriteLine(lst[0].ToString());
Console.ReadLine();
ArrayList
was a way to ease Java programmers into C# when C# was first being introduced (as well as the fact that generics were not yet introduced into the language).ArrayList
s is that they suffer from boxing and unboxing overhead if you are storing value types (e.g. simple types like integers, characters, doubles, structs) inside it. Since the ArrayList
internally uses an array of object
s, the runtime needs to wrap up a simple type with an object and then put a pointer to that object inside the list. Later when you need read an item from the array list, it need to get the object and then get the simple type out of the object.That could just be legend. Given who created C#, there'd be no surprise if it had Java-like features. The ArrayList class is part of .NET though, not just C#. Looking at it from a VB perspective, the old VB6 Collection class can behave like an ArrayList or a Hashtable. VB6 had a number of components that have .NET equivalents broken up over multiple classes so, logically, the concept could just as easily have come from VB. The name suggests at least a little Java influence though.My understanding is that theArrayList
was a way to ease Java programmers into C# when C# was first being introduced (as well as the fact that generics were not yet introduced into the language).
ArrayList
is to List<T>
as HashTable
is to Dictionary<TKey, TValue>
.Indeed. Where you would previously have used members of the System.Collections namespace or maybe System.Collections.Specialized, e.g. StringCollection, now you should use members of the System.Collections.Generic namespace. I always encourage people to read documentation and it's not a bad idea to check out the documentation for that namespace to see what's available. I've seen many a question posted online that could have been avoided if that had been done. You don't have to be solving a specific problem to read documentation. Finding information that may be useful later is one of the best things about reading documentation.And just to preempt your potential follow up question,ArrayList
is toList<T>
asHashTable
is toDictionary<TKey, TValue>
.
KeyedByTypeCollection<TItem> | Provides a collection whose items are types that serve as keys. |
SynchronizedCollection<T> | Provides a thread-safe collection that contains objects of a type specified by the generic parameter as elements. |
While List<T> is the generic collection, a List<string> or List<int> for example are strongly typed collections you create from it - but here I think they meant StringCollection that was mentioned or a strongly typed collection you write yourself by implementing System.Collections.IList.What are non-generic strongly typed collections?
object
a value or a key. As noted above, when you use an object
to store things, you could run into the boxing and unboxing overhead. Furthermore, if a collection took just an object
, you don't get the benefit of type-safety. Without type safety, you could do something as foolish as:var arrayList = new ArrayList();
arrayList.Add(new Human("Luke Skywalker"));
arrayList.Add(new Human("Han Solo"));
arrayList.Add(new Robot("Data")); // oops!
foreach(Human human in arrayList)
Console.WriteLine(human);
void *
to hold on to items in the list, it's essentially the same problem of lack of type safety. You have no guarantees that everything inside the list are the same type.I already gave you an example: System.Collections.Specialized.StringCollection.What are non-generic strongly typed collections?
using System.Collections.ObjectModel;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class TextBoxCollection : Collection<TextBox>
{
}
}
using System;
using System.Collections;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class TextBoxCollection : CollectionBase
{
public TextBox this[int index]
{
get => (TextBox) List[index];
set => List[index] = value;
}
public int Add(TextBox value)
{
return List.Add(value);
}
public int IndexOf(TextBox value)
{
return List.IndexOf(value);
}
public void Insert(int index, TextBox value)
{
List.Insert(index, value);
}
public void Remove(TextBox value)
{
List.Remove(value);
}
public bool Contains(TextBox value)
{
// If value is not of type TextBox, this will return false.
return List.Contains(value);
}
protected override void OnInsert(int index, object value)
{
// Insert additional code to be run only when inserting values.
}
protected override void OnRemove(int index, object value)
{
// Insert additional code to be run only when removing values.
}
protected override void OnSet(int index, object oldValue, object newValue)
{
// Insert additional code to be run only when setting values.
}
protected override void OnValidate(object value)
{
if (value is TextBox)
throw new ArgumentException("value must be of type TextBox.", nameof(value));
}
}
}