Arraylist是类型安全还是强类型?

Red*_*wan 5 .net arraylist strong-typing typesafe

我不知道"强类型"和"类型安全"的区别究竟是什么!

你能用一种简单的语言澄清一下吗?

假设我们正在使用Arraylist,但我无法断定它是类型安全的还是强类型的.或者我们可以同时使用它吗?

Dav*_*ych 8

一个ArrayList不是类型安全的.这意味着ArrayList可以为任何类型分配值:

ArrayList myList = new ArrayList();
myList.Add("this is a string");
myList.Add(19); //Notice that this is an int, but it doesn't throw an error!
Run Code Online (Sandbox Code Playgroud)

这是一个问题,因为当您使用该列表时,您不知道列表中的类型.抛出错误的可能性非常高.

避免使用ArrayLists!请改用通用列表,例如List<T>


Joe*_*orn 7

类型安全和强类型是加载的短语,因此很难让各地的程序员确定精确的定义.但至少有足够的共识表明ArrayLists 并不是在各方面都很重要.

不要使用ArrayLists!而是使用通用List<T>集合.