无法将 null 转换为类型参数“T1”,因为它可能是值类型。考虑使用“default(T)”代替

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

我有一个非常奇怪的错误。这是 MRE:

var outputs = new (
    (Typing First, Typing Second) a,
    (Typing First, Typing Second) b,
    (Typing First, Typing Second) c,
    (Typing First, Typing Second) d,
    (Typing First, Typing Second)? e,
    (Typing First, Typing Second) f,
    (Typing First, Typing Second)? g,
    (Typing First, Typing Second)? h
)[2];

outputs[0] = (
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing())
);

outputs[0].h = null;  // Cannot convert null to type parameter 'T1' because it could be a value type. Consider using 'default(T)' instead.

class Typing
{
}

您可能会问为什么这个 MRE 如此“不是最小的”。好吧,如果我使元组变小(例如,通过删除

f
g
),我就不再有错误了。

证明:

错误: enter image description here 没有错误: enter image description here

您可能需要将其添加到您的 csproj 才能看到错误:

<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors> <!-- Treating Nullable Warnings as Errors -->

详情:

JetBrains Rider 2024.3.3
Build #RD-243.22562.250, built on December 23, 2024
Licensed to **************************
Subscription is active until *******************
Runtime version: 21.0.5+8-b631.28 amd64 (JCEF 122.1.9)
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Toolkit: sun.awt.windows.WToolkit
Windows 11.0
.NET Core v8.0.4 x64 (Server GC)
GC: G1 Young Generation, G1 Concurrent GC, G1 Old Generation
Memory: 4078M
Cores: 16
Registry:
  ide.experimental.ui=true
  i18n.locale=
  llm.show.ai.promotion.window.on.start=false
Non-Bundled Plugins:
  com.jetbrains.space (243.21565.122)
  com.chesterccw.excelreader (2024.11.1-243)
  me.lensvol.blackconnect (0.6.2)
  com.github.copilot (1.5.30-242)
  com.intellij.resharper.azure (4.3.8)
c# rider
1个回答
0
投票

这是 JetBrains 的扩展问题。我在通常的.net环境中尝试过并且成功。对于您的情况,有 2 个临时解决方案,直到问题得到修复。尝试一下并在尝试让我知道后回复我

var outputs = new (
(Typing First, Typing Second) a,
(Typing First, Typing Second) b,
(Typing First, Typing Second) c,
(Typing First, Typing Second) d,
(Typing First, Typing Second)? e,
(Typing First, Typing Second) f,
(Typing First, Typing Second)? g,
(Typing First, Typing Second)? h)[2];

outputs[0] = (
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()),
    (new Typing(), new Typing()));

//First solution
//Same as null
outputs[0].h = default;
//Output : True
Console.WriteLine(outputs[0].h is null);

另一种方法是:

//Another solving. NOTE : insert your warning. Examples of code warning CS0168 CS8600 CS8602 CS8603
//Start disable warning region
#pragma warning disable YourCodeWarning
//Expression shouldn't show any related warning
outputs[0].h = null;
//End disable warning region
#pragma warning restore YourCodeWarning
© www.soinside.com 2019 - 2024. All rights reserved.