wps自动识别目录错误_使用net 5自动识别代码中的错误

wps自动识别目录错误

C#概念 (C# CONCEPTS)

.Net SDK now incorporates rich diagnostics and code recommendations in the .NET SDK by default.

.Net SDK现在默认情况下在.NET SDK中包含了丰富的诊断和代码建议。

In the past, it’s unwilling to add new warnings to C#. Its because adding recent warnings is technically a source breaking change for users who have notifications set as errors.

过去,它不愿意向C#添加新的警告。 这是因为从技术上来说,添加最近的警告对于将通知设置为错误的用户来说是一项重大的源变更。

C# compiler uses Analysis Level to introduce warnings for these patterns in a safe way.

C#编译器使用分析级别以安全的方式为这些模式引入警告。

Analysis Level is bound to the target framework of your project except you manually change what your project targets. SDK allows you to set your analysis level, though visual studio manually.

除您手动更改项目目标外,“ 分析级别”已绑定到项目的目标框架。 通过SDK,您可以通过Visual Studio手动设置分析级别。

Example of Analysis Level 5

分析级别5的示例

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework><AnalysisLevel>5</AnalysisLevel>
</PropertyGroup></Project>

If you always choose to be on the highest supported analysis level by default, then specify “latest” in your project file:

如果您始终默认选择处于最高支持的分析级别,则在项目文件中指定“ 最新”

<AnalysisLevel>latest</AnalysisLevel>

If you are comfortable with “preview” .Net versions

如果您对“ 预览” .Net版本感到满意

<AnalysisLevel>preview</AnalysisLevel>

Finally, the level “none,” which means “I don’t want to see any new warnings.” It’s beneficial if you are required to update your framework, but you’re not ready to absorb current warnings yet.

最后,级别为“ 无” ,表示“ 我不想看到任何新的警告。” 如果您需要更新框架,但是还没有准备好吸收当前警告,那将是有益的。

<AnalysisLevel>none</AnalysisLevel>

警告代码示例和相关解决方案 (Examples of warning codes & relevant solutions)

CS8073 —表达式始终为真或假时 (CS8073 — when the expression is always true or false)

This new warning is ubiquitous. Consider the following code:

这个新的警告无处不在。 考虑以下代码:

public void CheckDate(DateTime dateTime)
{
if (dateTime == null) // warning CS8073
{
return;
}
}

Datetime is of struct type & struct cannot be null. The warning message is:

日期时间为结构类型,结构不能为null。 警告消息是:

Warning CS8073: The result of the expression is always ‘false’ since the value of type ‘DateTime’ is never equal to ‘null’ of type ‘DateTime?’

警告CS8073:表达式类型的结果始终为'false',因为类型'DateTime'的值永远不会等于类型'DateTime?'的'null'。

(Solution)

Either remove the code or change its data type to “Datetime?” so that null is an intended value for the parameter.

删除代码或将其数据类型更改为“ Datetime?” 因此null为参数的预期值。

public void CheckDate(DateTime? dateTime) //null DateTime
{
if (dateTime == null) // No Warnings
{
return;
}
}

CS7023 —静态类型不允许“ as”或“ is” (CS7023 — Do not allow “as” or “is” on static types)

This next one is an excellent concise improvement:

下一个是一个非常简洁的改进:

static class ClassA
{
}class NonStaticClass
{
bool Test(object o)
{
return o is ClassA; // CS7023
}
}

Because ClassA is a static class, an instance object like “O” will never be able to be an instance of this type. Below warning will be triggered:

由于ClassA是静态类,因此“ O”之类的实例对象将永远无法成为该类型的实例。 以下警告将被触发:

Warning CS7023 The second operand of an ‘is’ or ‘as’ operator may not be static type ‘ClassA’

警告CS7023“ is”或“ as”运算符的第二个操作数可能不是静态类型“ ClassA”

(Solution)

Make the ClassA non-static:

使ClassA为非静态:

class ClassA
{
}class NonStaticClass
{
bool Test(object o)
{
return o is ClassA; // No Warning
}
}

CS0185 —不允许对非引用类型进行锁定 (CS0185 — Do not allow locks on non-reference types)

Locking a non-reference data type(i.e., int) does nothing because they are pass-by-value. In the past, warning popup for locking on non-reference types for simple cases like a lock(10), but until recently, the compiler does not warn you for open generics like below.

锁定非引用数据类型(即int)不会执行任何操作,因为它们是按值传递的。 过去,警告弹出窗口会针对诸如lock(10)之类的简单情况锁定非引用类型,但是直到最近,编译器才对以下类似的开放泛型不发出警告。

public class ClassA
{
public static void GetValue<TKey>(TKey key)
{
lock (key) // CS0185
{
}
}static void Main()
{
GetValue(1);
}
}

It’s an error because passing an int here will not lock correctly. The error will be displayed.

这是一个错误,因为在此处传递int不会正确锁定。 错误将显示。

Error CS0185 ‘TKey’ is not a reference type as required by the lock statement

错误CS0185'TKey'不是锁语句要求的引用类型

(Solution)

Apply generic constraints to indicate the reference types allowed for TKey.

应用通用约束以指示TKey允许的引用类型。

public class ClassA
{
public static void GetValue<TKey>(TKey key) where TKey : class
{
lock (key) // no error
{
}
}
}

了解有关通用约束的更多信息 (Learn more about Generic Constraints)

CA2013 —不要使用ReferenceEquals (CA2013 — Do not use ReferenceEquals)

Equality is a complex topic in .NET. Consider the code below:

平等是.NET中的一个复杂主题。 考虑下面的代码:

int int1 = 1;
int int2 = 1;//warning CA2013
Console.WriteLine(object.ReferenceEquals(int1, int2));

The ReferenceEquals method will always return false. We will see this warning description:

ReferenceEquals方法将始终返回false。 我们将看到以下警告说明:

Warning CA2013: Do not pass an argument with value type ‘int’ to ‘ReferenceEquals’. Due to value boxing, this call to ‘ReferenceEquals’ will always return ‘false’.

警告CA2013:请勿将值类型为'int'的参数传递给'ReferenceEquals'。 由于值装箱,对此“ ReferenceEquals”的调用将始终返回“ false”。

(Solution)

Either use the equality operator “==” or “object”.

使用相等运算符“ == ”或“ object ”。

int int1 = 1;
int int2 = 1;// using the equality operator
Console.WriteLine(int1 == int2);//No Warning
Console.WriteLine(object.Equals(int1, int2));

CA1831 —在适当的情况下,对字符串使用AsSpan而不是基于范围的索引器 (CA1831 — Use AsSpan instead of Range-based indexers for the string when appropriate)

class Program
{
public void Test(string str)
{
ReadOnlySpan<char> slice = str[1..3]; // CA1831
}
}

In the code above, it’s clear the developer means to index a string using the new range based index feature in C#. Unfortunately, this will allocate a string unless you convert that string to a span first.

在上面的代码,很明显开发商的手段指标使用新的字符串[R在C#安格基于索引功能。 不幸的是,除非您首先将该字符串转换为跨度,否则这将分配一个字符串。

Warning CA1831 Use ‘AsSpan’ instead of the ‘System.Range’-based indexer on ‘string’ to avoid creating unnecessary data copies

警告CA1831在“字符串”上使用“ AsSpan”而不是基于“ System.Range”的索引器,以避免创建不必要的数据副本

(Solution)

The fix is to add AsSpan calls in this case:

解决方法是在这种情况下添加AsSpan调用:

class Program
{
public void Test(string str)
{
ReadOnlySpan<char> slice = str.AsSpan()[1..3]; // no warning
}
}

CA2014 —请勿在循环中使用“ stackalloc” (CA2014 — Do not use “stackalloc” in loops)

The “stackalloc” keyword is great when you want to make sure the operations you are doing are easy on the garbage collector. In the past, “stackalloc” was only allowed in unsafe code, but since C# 8.0., it’s also been allowed outside of unsafe blocks.

当您要确保正在垃圾回收器上执行的操作很容易时,“ stackalloc”关键字非常有用 。 过去,仅在不安全的代码中才允许使用“ stackalloc” ,但自C#8.0。起,它也可以在不安全的块之外使用。

class ClassA
{
public void Test(string str)
{
int length = 3;
for (int i = 0; i < length; i++)
{
Span<int> numbers = stackalloc int[length]; // CA2014
numbers[i] = i;
}
}
}

Designating a lot on the stack can lead to the popular StackOverflow exception.

在堆栈上指定很多内容可能会导致流行的StackOverflow异常。

Warning CA2014 Potential stack overflow. Move the stackalloc out of the loop.

警告CA2014潜在的堆栈溢出。 将stackalloc移出循环。

(Solution)

Move “stackalloc” out of the loop.

将“ stackalloc”移出循环。

class ClassA
{
public void Test(string str)
{
int length = 3;
Span<int> numbers = stackalloc int[length]; // no warning
for (int i = 0; i < length; i++)
{
numbers[i] = i;
}
}
}

更多C#概念 (More C# Concepts)

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

感谢您的阅读。 继续访问并在您的网络中分享。 请在评论部分中输入您的想法和反馈。

Follow me on LinkedIn Instagram Facebook Twitter

跟随我的LinkedIn 的Instagram 的Facebook 的Twitter

翻译自: https://medium.com/swlh/automatically-identify-bugs-in-your-code-with-net-5-3ef803774f6

wps自动识别目录错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值