最好的方法,从动物[]转换为狗[] [英] Best way to cast from Animal[] to Dog[]

查看:77
本文介绍了最好的方法,从动物[]转换为狗[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果从动物狗enherits。

和我有一个动物[],我碰巧知道只包含犬。什么是让我的手在狗[]最快/最好的方法是什么?我用新的ArrayList(oldarray).ToArray(typeof运算(狗));到目前为止,但感觉有点笨拙,我想知道如果有什么更优雅。

更新:使用.NET 2.0配置文件。应该offcourse提到了这一点,立竿见影。我希望编辑原来的问题坚持在这种情况下,计算器网络礼仪。我期待着有一天,我们可以升级,使用Linq。

再见,卢卡斯


解决方案

  VAR dog_arr = Array.Convert(animal_arr,X =>(狗)x)的;

If Dog enherits from Animal.

And I have a Animal[], that I happen to know contains only dogs. What's the fastest/best way to get my hands on a Dog[] ? I've used new ArrayList(oldarray).ToArray(typeof(Dog)); so far, but that feels a bit clumsy, and I'm wondering if there is something more elegant.

UPDATE: Using the .net 2.0 profile. Should have offcourse mentioned this straight away. I hope editing the original question adheres to the stackoverflow netiquette in this case. I'm looking forward to the day where we can upgrade and use Linq.

Bye, Lucas

解决方案

var dog_arr = Array.Convert(animal_arr, x => (Dog) x);

这篇关于最好的方法,从动物[]转换为狗[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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