【发布时间】:2022-02-18 17:25:51
【问题描述】:
但是我尝试收到以下警告。
严重性代码描述项目文件行抑制状态 警告 CS8600 将 null 文字或可能的 null 值转换为不可为 null 的类型。
代码如下。
HttpResponseMessage response = await _httpClient.PutAsync(url, requestContent);
string? userResponse = await response.Content.ReadAsStringAsync();
JsonSerializerOptions? options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
//if (userResponse.Length > 0)
//{
user = JsonSerializer.Deserialize<GetUserById>(userResponse, options);
//}
更新。
我完全删除了 if 条件,但仍然收到警告。
更新 2
按照建议添加了空检查。但仍然收到该警告。
【问题讨论】:
-
您应该在检查长度之前检查 userResponse 是否为空
-
您在哪一行收到警告?
-
在下面查看我的答案。我在代码示例中添加了您缺少的空检查。
-
你必须使用你的字符串作为“字符串”吗? ? "string userResponse" 仍然可以为空,你不必使用 "string?" 来指定它