【问题标题】:Array does not contain a definition for 'AsReadOnly'数组不包含“AsReadOnly”的定义
【发布时间】:2021-10-28 16:58:13
【问题描述】:

我在 OS X 下将 C# 与 .NET Core 1.1 一起使用(目标框架是 netstandard1.6),但我收到一个缺少方法的奇怪错误。

using System;
using System.Collections.ObjectModel;

class Program
{
    static void Main()
    {
        int[] array = { 1, 2, 3 };
        ReadOnlyCollection<int> result = Array.AsReadOnly(array);
    }
}

上面显示的程序编译会报错

error CS0117: 'Array' does not contain a definition for 'AsReadOnly'

AsReadOnly 是否在 .NET Core 中不可用,还是我遗漏了其他内容?

【问题讨论】:

  • 基于docs.microsoft.com/en-us/dotnet/api/…,好像不支持。
  • 但是你总是可以做到new ReadOnlyCollection(array)
  • 实现只是return new ReadOnlyCollection&lt;T&gt;(array); 所以我想你可以这样做。
  • 好像不在netstandard1.6
  • 您使用的是哪个.NetFramework?

标签: c# .net-core


【解决方案1】:

The Array.AsReadOnly method 在 .Net Core 1.x 和 .Net Standard 1.x 中确实不存在。

它将包含在 .Net Core 2.0 和 .Net Standard 2.0 中。 与此同时,您可以使用new ReadOnlyCollection&lt;int&gt;(array),正如已经建议的那样。

【讨论】:

  • 我在 .Net Core 2.1 中仍然找不到 AsReadOnly,我遗漏了什么?
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 2018-01-27
  • 1970-01-01
相关资源
最近更新 更多