在Delphi XE中使用通用容器 - 总是? [英] Using Generic containers in Delphi XE - always?

查看:114
本文介绍了在Delphi XE中使用通用容器 - 总是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通用容器可以在有物品时节省时间,并且可以是这些物品的强类型列表。它可以节省创建一个可能具有TList内部变量的新类的重复编码,以及类型化的添加/删除类型方法等优点(例如通用容器类提供的所有新功能)。

然而,推荐总是使用泛型容器来进行强类型列表吗?这样做的具体缺点是什么? (如果不担心代码的向后兼容性)。我昨天正在编写一个服务器应用程序,并有一个我以旧方式创建的项目列表,并将用一个通用列表替换它,但决定保持它精简,但大多出于习惯。 (我们是否应该打破这种习惯,并总是使用泛型来开始一个新习惯?)


<不要使用泛型容器。



通过转换旧方法将会给你:

    $ b $
  • 枚举器,用于循环中

  • 相同的清理器,类型安全,不易出错的代码, / strike>更好的性能特征。


Generic containers can be a time saver when having a item, and a strongly typed list of those items. It saves the repetitive coding of creating a new class with perhaps a TList internal variable, and typed Add/Delete type methods, among other benefits (such as all the new functionality provided by the Generic container classes.)

However, is it recommended to always use generic containers for strongly typed lists going forward? What are the specific downsides of doing so? (If not worried about backwards compatibility of code.) I was writing a server application yesterday and had a list of items that I created 'the old way' and was going to replace it with a generic list but decided to keep it lean, but mostly out of habit. (Should we break the habit and start a new one by always using generics?)

解决方案

In Delphi XE, there is no reason not to use generic containers.

Switching from the old method with casting will give you:

  • cleaner, type-safe, less error-prone code,
  • enumerators, for in loops,
  • the samebetter performance characteristics.

这篇关于在Delphi XE中使用通用容器 - 总是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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