C#中的那些警告该如何去除(完全去除C#警告)
作者:河西石头 时间:2023-09-07 20:55:53
前言
一般在c++中没有使用的变量会有警告,C#中也有,在QT中我们利用Q_UNSED可以直接消除这些警告,那么我们在C#中该如何做才能消除这些不必要的警告呢?经过查询微软官方网站发现,有的找到了解决方法,有的仍然一筹莫展,,网络上没有任何关于消除警告的方法,于是只能自己慢慢琢磨,经过一番努力,可以说找到了消除这些警告“钉子户”的方法,这里分享出来,给那些和我一样讨厌看到这些警告的朋友们。
1、在项目配置文件中去掉Nullable描述
2、给变量增加isnull条件判断
void GetMessageLength(string? message)
{
if (message is not null)
{
Console.WriteLine(message.Length);
}
}
public void GetMessage(string? message)
{
if (IsNotNull(message))
Console.WriteLine(message.Length);
}
private static bool IsNotNull([NotNullWhen(true)] object? obj) => obj != null;
3、给变量或属性设置可为null
private string _name = null!;
public DbSet<TodoItem> TodoItems { get; set; } = null!;
4、在参数类型后增加?
一种是添加 ? 注释,使变量成为可以为 null 的引用类型
void IList.Insert(int index, object ?value)
{
if (value != null)
this.Insert(index, (Animal)value);
}
object? IList.this[int index]
{
get
{
return _list[index]!;
}
set
{
_list[index] = (Animal?)value;//(Animal)value;
}
}
5、在参数后增加!
将 null 包容性运算符 ! 添加到右侧:
string msg = TryGetMessage(42)!;
return msg!;
Console.WriteLine(msg!.Length);
6、使用empty
public class Person
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
}
7、给参数使用NotNullWhen时配合显式null
public bool TryGetMessage(int id, [NotNullWhen(true)] out string? message)
{
message = null;
return true;
}
8、set设置器警告
就算这样,我们发现,有的set的设置器的警告还是没有办法去掉,这个时候我们可以在返回类型上加上“!”即可,如下图:
还有这种情况,看起来是构造函数被警告了,实际为属性的问题
public class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
我们可以参考的官方说明:点击这里
来源:https://blog.csdn.net/haigear/article/details/128773911
标签:c#,警告,去除
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring boot日志管理配置
2023-01-09 16:50:04
C# XML基础入门小结(XML文件内容增删改查清)
2022-10-18 17:12:02
Java日常练习题,每天进步一点点(8)
2022-04-15 16:40:26
![](https://img.aspxhome.com/file/2023/9/61879_0s.jpg)
android开发之Json文件的读写的示例代码
2021-12-02 11:55:18
![](https://img.aspxhome.com/file/2023/2/84712_0s.png)
Java 实战范例之线上婚纱摄影预定系统的实现
2021-08-08 14:19:42
![](https://img.aspxhome.com/file/2023/4/61384_0s.png)
Spring数据访问模板化方法
2022-03-15 06:23:02
![](https://img.aspxhome.com/file/2023/0/129950_0s.png)
MyBatis插入数据返回主键的介绍
2023-10-26 03:15:21
springboot异常处理的基本规范
2023-11-23 15:48:19
![](https://img.aspxhome.com/file/2023/8/59488_0s.png)
android中AutoCompleteTextView的简单用法(实现搜索历史)
2023-01-08 18:33:01
![](https://img.aspxhome.com/file/2023/0/125430_0s.jpg)
Spring Boot 入门教程
2023-05-26 00:14:44
Java内部类及其特点的讲解
2023-06-09 19:39:41
![](https://img.aspxhome.com/file/2023/7/61657_0s.png)
Java中Scanner类与BufferReader类的不同点(非常详细)
2023-07-07 01:21:18
springboot 同时启用http/https的配置方法
2023-06-22 12:04:12
![](https://img.aspxhome.com/file/2023/2/108422_0s.png)
java实现通过绑定邮箱找回密码功能
2021-12-17 00:16:48
![](https://img.aspxhome.com/file/2023/0/67660_0s.png)
MyBatis-Plus 集成动态多数据源的实现示例
2023-06-26 17:56:35
Unity TextMeshPro实现富文本超链接默认字体追加字体
2021-10-13 23:57:24
![](https://img.aspxhome.com/file/2023/1/85671_0s.png)
Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案
2022-10-30 00:31:51
Java 高并发五:JDK并发包1详细介绍
2022-11-29 08:56:26
![](https://img.aspxhome.com/file/2023/4/62814_0s.gif)
c#代码自动修改解决方案下任意文件实例
2023-01-31 07:27:10
详解c# 委托链
2021-10-06 23:17:59
![](https://img.aspxhome.com/file/2023/0/77300_0s.png)