为什么Visual Studio将新创建的数组键入为Nullable? [英] Why does Visual Studio Type a Newly Minted Array as Nullable?

查看:125
本文介绍了为什么Visual Studio将新创建的数组键入为Nullable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写具有通用类型TVal的函数.我写了这一行:

I'm writing a function with a generic type TVal. I wrote this line:

var zeroBased = new TVal[size];

然后在Visual Studio(VS)中,我使用alt + enter将var替换为显式类型.这是我得到的:

And then in Visual Studio (VS) I used alt+enter to replace var with an explicit type. Here's what I got:

TVal[]? zeroBased = new TVal[size];

我很惊讶地发现?运算符,指示该类型可能为空.我认为使用new创建该类型时永远不会为null,并且这样做可能会很安全:

I was surprised to find the ? operator, indicating that the type might be nullable. I thought that I'd be safe enough assuming the type is never null when created with new, and could have just done:

TVal[] zeroBased = new TVal[size];

是否存在一种情况,在C#中实例化一个新数组可以使您返回null?

Is there a scenario where instantiating a new array in C# can give you back null?

注意:如果没有?,代码似乎可以正常编译,我只是对VS的建议很感兴趣...

Note: the code seems to compile fine without the ?, I'm just intrigued by VS's suggestion...

打开Visual Studio(与下面指定的版本相同),创建一个新项目,根据下面的VS Project文件内容启用可为空的类型,创建一个新类,然后弹出此函数:

Open Visual Studio, same version as specified below, create a new project, enable nullable types as per the VS Project File Contents below, create a new class, and pop in this function:

public void Test<T>(int size)
{
  var tArr = new T[size];
}

选择var并单击alt+enter,然后选择将var替换为显式类型.如果行为与我所经历的相同,您将得到:

The select the var and hit alt+enter, and choose to replace var with explicit type. If the behaviour is the same as the one I experienced, you'll get:

public void Test<T>(int size)
{
  T[]? tArr = new T[size];
}

Visual Studio项目文件内容

我们正在为该项目使用C#8,并且启用了Nullables:

Visual Studio Project File Contents

We're using C# 8 for this project and we've enabled Nullables:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Nullable>enable</Nullable>
    <LangVersion>8.0</LangVersion>
    <WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
    <TargetFramework>netstandard2.0</TargetFramework>
    <OutputType>Library</OutputType>
    <Version>1.0.0.9</Version>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
  </ItemGroup>

</Project>

Visual Studio版本信息(对于本季度来说似乎很重要的位)

Microsoft Visual Studio社区2019 版本16.6.1 VisualStudio.16.发行版/16.6.1+30128.74 Microsoft .NET Framework 版本4.7.03062

Visual Studio Version Info (just bits that seemed important to this Q)

Microsoft Visual Studio Community 2019 Version 16.6.1 VisualStudio.16.Release/16.6.1+30128.74 Microsoft .NET Framework Version 4.7.03062

安装版本:社区

C#工具3.6.0-4.20251.5 + 910223b64f108fcf039012e0849befb46ace6e66 IDE中使用的C#组件.根据您的项目类型和设置,可以使用其他版本的编译器.

C# Tools 3.6.0-4.20251.5+910223b64f108fcf039012e0849befb46ace6e66 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

推荐答案

带有C#8的Visual Studio允许您根据在项目中设置的上下文使用可为空的类型.您可以在此处找到文档.

Visual Studio with C# 8 allows you to use nullable types according to the contexts you setup in your project. You can find docs Here.

启用它的一种方法是在项目文件中使用<Nullable>enable</Nullable>条目.如果有,那么在转换为显式变量时,它将选择使用可为空的类型.

One way to enable it is with a <Nullable>enable</Nullable> entry in your project file. If you have that then it'll choose to use the nullable type when you convert to explicit variable.

我不确定是否可以将相同的行为用于其他方式(例如编译指示)来启用它.我只尝试了项目文件方法.

I'm not sure if that same behavior would be used for the other ways - pragmas for example - to enable it. I only tried the project file method.

这篇关于为什么Visual Studio将新创建的数组键入为Nullable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆