是否可以在C# Interactive上使用Immutable.Collections?

问题描述 投票:0回答:1

我正在使用Visual Studio 2019上的C# Interactive窗口(即REPL),并有最新的更新。我想使用不可变的集合,但无法让它们工作。 我在编译的C#项目中使用它们没有问题)。

奇怪的是,如果我输入(进入C#互动窗口)。

using System.Collections.Immutable;

C# Interactive接受了这一点,意味着它可以看到命名空间。但是,如果我尝试使用它,例如。

var a = ImmutableList.Create<int>(1,2,3);

我得到的错误信息是: 'The name 'ImmutableList' does not exist in the current context'。 如果我完全限定ImmutableList的名称,那就没有区别了。

可能是我没有理解.NET C# Interactive是针对哪个版本工作的。 我知道Immutable.Collections现在在.NET Core 3中是原生的,但也许C# Interactive没有使用这个版本?(如何在C# Interactive中告诉pecify.NET版本?)。 即使如此,它没有在使用指令时提出抗议,这似乎很奇怪。

c# visual-studio-2019 read-eval-print-loop immutable-collections
1个回答
3
投票

你需要引用 System.Collections.Immutable 首先是装配体。要做到这一点,使用这个。

#r "System.Collections.Immutable.dll"

现在你可以使用 System.Collections.Immutable 命名空间。

© www.soinside.com 2019 - 2024. All rights reserved.