We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
https://www.youtube.com/watch?v=v7GAQfWnco0&ab_channel=NickChapsas
The text was updated successfully, but these errors were encountered:
dotnet/runtime#78442
Sorry, something went wrong.
在 .NET 8 中, Code Analysis 为 C# 代码更多的检查,比如下面 6 种例子
.NET 8
Code Analysis
public static void ProcessValue([ConstantExpected (Min = 20)]int value) { }
如果方法的参数增加了 ConstantExpected 的注解,那么要求方法的调用必须传入常量
ConstantExpected
public static bool HasElement(string[] strings) { return strings.Any(); }
通常 Linq 中的 Any() 方法可以判断是否存在元素,但是如果是具体的类型,比如 Array, List 等,则分析器提示使用 strings.Length !=0 的方式。
Linq
Any()
Array
List
strings.Length !=0
public static string[] SplitText(string text) { return text.Split(new char[] { ' ', ',' }); }
在这个方法中,每次调用 SplitText 方法,都会创建一个 char[] 对象,这样增加了内存压力,所以分析器建议将 array 对象设置为一个只读对象,并且只初始化一次。
SplitText
char[]
var test = new int[] { 1 }.Cast<string>();
这个代码在运行的时候会抛出 InvalidCastExpection ,现在分析器会检测到这个错误并且在编译的时候给出这样提示。
InvalidCastExpection
在 Runtime repo 中的 issue,它列出可以增加的分析功能。
No branches or pull requests
https://www.youtube.com/watch?v=v7GAQfWnco0&ab_channel=NickChapsas
The text was updated successfully, but these errors were encountered: