【问题标题】:Implicitly convert an object array to an int array in c#在 C# 中将对象数组隐式转换为 int 数组
【发布时间】:2020-07-02 06:08:21
【问题描述】:

我喜欢将类数组转换为整数数组。我的Data 班级是这样的-

public class Data
{
    public int low, high;

    ...................
    ...................

    public static implicit operator int[](Data myClass) 
    {
        int[] arr = {myClass.low, myClass.high};
        return arr;
    }
}

在这个class 中,我有一个implicit operator conversion 函数,以便可以将类转换为整数数组。

我正在创建Data 类列表对象并将列表转换为int 这样的二维数组-

 public int[][] Merge(int[][] intervals) {
    List<Data> list = new List<Data>();

    ............
    ............

    return list.Cast<int[]>().ToArray();
}

然后我得到这个错误-

System.InvalidCastException: Specified cast is not valid.
at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
Line 0: System.Linq.Enumerable+&lt;CastIterator&gt;d__34`1[TResult].MoveNext () in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 0: System.Collections.Generic.LargeArrayBuilder`1[T].AddRange (System.Collections.Generic.IEnumerable`1[T] items) in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 0: System.Collections.Generic.EnumerableHelpers.ToArray[T] (System.Collections.Generic.IEnumerable`1[T] source) in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 0: System.Linq.Enumerable.ToArray[TSource] (System.Collections.Generic.IEnumerable`1[T] source) in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 58: Solution.Merge (System.Int32[][] intervals) in Solution.cs
Line 21: __DriverSolution__.__Helper__ (System.Int32[][] param_1) in __Driver__.cs
Line 37: __Driver__.Main (System.String[] args) in __Driver__.cs
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Specified cast is not valid.
at (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
Line 0: System.Linq.Enumerable+&lt;CastIterator&gt;d__34`1[TResult].MoveNext () in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 0: System.Collections.Generic.LargeArrayBuilder`1[T].AddRange (System.Collections.Generic.IEnumerable`1[T] items) in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 0: System.Collections.Generic.EnumerableHelpers.ToArray[T] (System.Collections.Generic.IEnumerable`1[T] source) in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 0: System.Linq.Enumerable.ToArray[TSource] (System.Collections.Generic.IEnumerable`1[T] source) in &lt;59093ef301a24e5a91cb0295fb832cca&gt;
Line 58: Solution.Merge (System.Int32[][] intervals) in Solution.cs
Line 21: __DriverSolution__.__Helper__ (System.Int32[][] param_1) in __Driver__.cs
Line 37: __Driver__.Main (System.String[] args) in __Driver__.cs

谁能帮我解决这个问题?

【问题讨论】:

  • 我认为您提供的代码不会引发错误。堆栈跟踪似乎表明您在合并函数的某处调用list.Sort(),这就是导致错误的原因。您可能需要实现IComparable 中提到的this answer
  • 请检查我得到的更新错误

标签: c# asp.net arrays asp.net-core implicit-conversion


【解决方案1】:

我猜这个异常无关紧要,可能在代码到达Cast&lt;int[]&gt;()之前被抛出,但我可以看到即使代码可以到达Cast&lt;int[]&gt;,仍然有InvalidCastException错误。

运算符是一个编译器技巧。

int[] array = new Data();

上面的工作是因为编译器将=的右操作数替换为对运算符方法的方法调用。

int[] array = Data.op_Implicit(new Data());

当您使用Cast&lt;T&gt; 时,您没有编译器帮助您执行上述操作,因此根本不会调用运算符。

为了能够投射,您需要使用Select 方法并像这样显式投射:

int[][] array = list.Select(x => (int[])x).ToArray();

【讨论】:

  • 我需要int[][],而不是int[]
  • @AbrarJahin 糟糕,这是一个错字,它返回 int[][] 而不是 int[]
猜你喜欢
  • 2021-07-31
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多