小编pav*_*vok的帖子

如何在类中初始化不可为空的字符串?

我有这个项目:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.1" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

这是主要的类:

namespace CSharp8
{
    using System;
    using Microsoft.Extensions.Configuration;

    internal class Program
    {
        private static void Main()
        {
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", false, true)
                .Build();
            var appSettings = configuration.Get<AppSettings>();
        }

        public class AppSettings
        {
            public string SourceServer { get; set; }
            public string TargetServer { get; set; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是appsettings.json:

{
  "SourceServer": "prodtools",
  "TargetServer": "qatools"
}
Run Code Online (Sandbox Code Playgroud)

在 AppSettings 的属性上,我收到此警告:

警告 CS8618 不可为 …

c# string initialization non-nullable

5
推荐指数
1
解决办法
2248
查看次数

标签 统计

c# ×1

initialization ×1

non-nullable ×1

string ×1