如何在C#中将IList转换为ArrayList? [英] How can I convert IList to ArrayList in C#?

查看:312
本文介绍了如何在C#中将IList转换为ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友您好,



如何使用C#将IList转换为ArrayList?



请帮助

解决方案

谷歌的魔力..... [ ^ ]


列表< string> l =  new  List< string>(); 
l.Add( one);
l.Add( two);
l.Add( three);
l.Add( four);
l.Add( five);

string [] s = l.ToArray();
ArrayList myArrayList = new ArrayList(s.Length);
myArrayList.AddRange(s);


首先,您应该按照转换的方式设计代码(但在这种情况下,这个不是转换,但是从接口类型到接口实现类型的类型转换永远不需要。如果你清楚地预期你的目的,我可能会解释如何在你的例子中避免它。 />


其次,你不应该在新开发中使用 ArrayList 。这种类型早在v时就已经过时了.2.0,当引入泛型时。你应该使用强类型方法和泛型类型 System.Collections.Generic.List<> 或其他通用集合类型。请参阅:

http://msdn.microsoft.com/en-us/library/ 6sh2ey19.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/System.Collections.Generic.aspx [ ^ ]。



这个类没有正式标记为过时,因为在遗留项目中使用它并没有任何问题,否则它与新版本的.NET框架向后兼容。对于新的开发它根本没有意义,因为没有强类型的集合需要类型的情况,这是一个额外的潜在的错误来源。



-SA

Hello friends,

How can I convert an IList to an ArrayList using C#?

Please help

解决方案

The magic of google.....[^]


List<string> l = new List<string>();
    l.Add("one");
    l.Add("two");
    l.Add("three");
    l.Add("four");
    l.Add("five");

    string[] s = l.ToArray();
    ArrayList myArrayList = new ArrayList(s.Length);
    myArrayList.AddRange(s);


First of all, you should design your code the way the "conversion" (but in this case this is not "conversion" but type cast from interface type to interface implementing type is never needed. If you clearly expected your purpose, I would probably be able to explain how to avoid it on your example.

Second of all, you should not use ArrayList in new development. This type is rendered obsolete as early as of v.2.0, when generics were introduced. You should use strongly typed approach with the generic type System.Collections.Generic.List<> or other generic collection types. Please see:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^],
http://msdn.microsoft.com/en-us/library/System.Collections.Generic.aspx[^].

This class wasn't formally marked obsolete only because there is nothing wrong with using it in legacy project which are otherwise backward-compatible with newer versions of the .NET framework. For new development it simply makes no sense, as not strongly typed collections require type case, which is an extra potential source of bugs.

—SA


这篇关于如何在C#中将IList转换为ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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