【问题标题】:converting an unknown long\int\short to double in C#在 C# 中将未知的 long\int\short 转换为 double
【发布时间】:2014-02-05 08:00:32
【问题描述】:

在我的应用程序中,我有一个未知类型的列表,可能是 int\long\short。

我需要将此列表转换为双精度。

实现这一目标的最快和最有效的方法是什么? (它需要尽可能快)

【问题讨论】:

  • List<unknwon type> 表示List<object>?为什么不使用类型T 表示类型的泛型方法?
  • 你是如何得到这份名单的?数据从何而来?
  • 动态是将类型转换推送到运行时的最佳方式
  • @Arun,不,真的不是。

标签: c# double type-conversion


【解决方案1】:

试试这个

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

【讨论】:

  • @SriramSakthivel 阅读了仅针对int\long\short 而不是object 提出的问题
  • 你又看了一遍问题! 在我的应用程序中,我有一个未知类型列表,可能是 int\long\short。 如果您不知道类型,则必须存储类型仅限List&lt;Object&gt;。在这种情况下,您的答案将不起作用。清除吗?
【解决方案2】:

我假设您有List&lt;object&gt;,您需要将其转换为List&lt;double&gt;

试试这个,这将适用于所有实现IConvertible 的类型。 longintshortfloat等...

var doubleList = objectList.Select(x=> Convert.ToDouble(x)).ToList();

【讨论】:

    【解决方案3】:

    非常简单:

    var doubleList = listOfObjects.Select(i => Convert.ToDouble(i)).ToList();
    

    微优化,因为您说“最高效”很重要:

    int count = listOfObjects.Count;
    var doubleList = new List<double>(listOfObjects.Count);
    for(int i = 0; i != count; ++i)
      doubleList.Add(Convert.ToDouble(listOfObjects[i]));
    

    但是,“最高效”取决于您需要它在什么方面最高效。您可以通过以下方式获得不同的效率:

    public class DoubleList : IList<double>
    {
      private readonly List<object> _source; // Change to IList<object> if that's a possibility
      public DoubleList(List<object> source)
      {
        _source = _source;
      }
      // Hide half-supported implementation from main interface
      double IList<double>.this[int index]
      {
        get { return Convert.ToDouble(_source[index]); }
        set { throw new NotSupportedException("Read-only collection"); }
      }
      public double this[int index]
      {
        get { return Convert.ToDouble(_source[index]); }
      }
      public int Count
      {
        get { return _source.Count; }
      }
      bool ICollection<double>.IsReadOnly
      {
        get { return true; }
      }
      /* Lots of boring and obvious implementations skipped */
      public struct Enumerator : IEnumerator<double>
      {
        // note, normally we'd just use yield return in the
        // GetEnumerator(), and we certainly wouldn't use
        // a struct here (there are issues), but this
        // optimisation is in the spirit of "most efficient"
        private List<object>.Enumerator _en; //Mutable struct! Don't make field readonly!
        public double Current
        {
          get { return Convert.ToDouble(_en.Current); }
        }
        object IEnumerator.Current
        {
          get { return Current; }
        }
        public void Dispose()
        {
          _en.Dispose();
        }
        public bool MoveNext()
        {
          return _en.MoveNext();
        }
        public void Reset()
        {
          _en.Reset();
        }
      }
      public Enumerator GetEnumerator()
      {
        return new Enumerator(_source.GetEnumerator());
      }
      IEnumerator<double> IEnumerable<double>.GetEnumerator()
      {
        return GetEnumerator();
      }
      IEnumerator IEnumerable.GetEnumerator()
      {
        return GetEnumerator();
      }
    }
    
    var doubleList = new DoubleList(listOfObjects);
    

    这会围绕发生的事情发生变化,从而改变成本。您将在恒定时间内返回,但实际上使用列表会更昂贵。但是,如果您只查看几个字段,或者可能只获取计数然后枚举它,那么它不会执行完整副本这一事实可以使其效率更高。

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 1970-01-01
      • 2013-03-15
      • 2013-09-25
      • 1970-01-01
      • 2013-01-20
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多