Arraylist 是类型安全的还是强类型的? [英] Is Arraylist is typesafe or strongly typed?

查看:28
本文介绍了Arraylist 是类型安全的还是强类型的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道强类型"和类型安全"之间究竟有什么区别!

I don't know what exactly the difference between "Strongly typed" and "Type safety" is!

你能用简单的语言解释一下吗?

Could you please clarify this in a simple language?

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

Suppose we are using Arraylist, but I am unable to conclude it is typesafe or strongly typed. or can we use it as both.?

推荐答案

ArrayList 不是类型安全的.这意味着 ArrayList 可以被赋予任何类型的值:

An ArrayList is not typesafe. What this means is that ArrayList can be assigned a value of any type:

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!

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

This is an issue because when you go to use the list, you don't know the types that are in the list. The chances of having an error thrown are very high.

避免使用ArrayLists!改用通用列表,例如 List

Avoid using ArrayLists! Use a generic list instead, such as List<T>

这篇关于Arraylist 是类型安全的还是强类型的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆