English 中文(简体)
C# Converting List<int> to list<double>
原标题:C# Converting List<int> to List<double>
  • 时间:2010-01-18 07:32:21
  •  标签:
  • c#
  • generics

我有<代码>List<int>,我希望将其改为List<double>。 除了通过<代码>List<int>和在新的<代码>List<double>上添加以下内容外,还有其他办法:

List<int> lstInt = new List<int>(new int[] {1,2,3});
List<double> lstDouble = new List<double>(lstInt.Count);//Either Count or Length, I don t remember

for (int i = 0; i < lstInt.Count; i++)
{
    lstDouble.Add(Convert.ToDouble(lstInt[0]));
}

是否有办法这样做? 使用C# 4.0的Im,因此答案可能利用新的语言特征。

最佳回答

您可使用LINQ方法:

List<double> doubles = integers.Select<int, double>(i => i).ToList();

List<double> doubles = integers.Select(i => (double)i).ToList();

此外,清单类别还采用了每种方法:

List<double> doubles = new List<double>(integers.Count);
integers.ForEach(i => doubles.Add(i));
问题回答

http://www.un.org/Depts/DGACM/index_french.htm ConvertAll:

List<double> doubleList = intList.ConvertAll(x => (double)x);

这有两个好处:

  • It doesn t require LINQ, so if you re using .NET 2.0 and don t want to use LINQBridge, you can still use it.
  • It s more efficient: the ToList method doesn t know the size of the result of Select, so it may need to reallocate buffers as it goes. ConvertAll knows the source and destination size, so it can do it all in one go. It can also do so without the abstraction of iterators.

缺点:

  • It only works with List<T> and arrays. If you get a plain IEnumerable<T> you ll have to use Select and ToList.
  • If you re using LINQ heavily in your project, it may be more consistent to keep using it here as well.

http://msdn.microsoft.com/en-us/library/bb548891.aspx“rel=“noreferer”>。

List<double> doubleList = intList.Select(x => (double)x).ToList();

您可在“Net Framework 2.0”内使用“ConvertAll”方法,就是一个例子。

        List<int> lstInt = new List<int>(new int[] { 1, 2, 3 });
        List<double> lstDouble = lstInt.ConvertAll<double>(delegate(int p)
        {
            return (double)p;
        });

您可以使用一种方法:

lstDouble = lstInt.Select(Convert.ToDouble)

你们可以使用选择性的或变式的。 牢记2.Net 可供查询





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签