我得到CA1820“使用字符串长度测试空字符串” [英] I get CA1820 "Test for empty string using string length"

查看:84
本文介绍了我得到CA1820“使用字符串长度测试空字符串”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行代码分析时,我得到了CA1820并希望看看是否有更好的选项来编写以下代码块。





When I run code analysis I get CA1820 and wanted to see if there are better options for writing the following block of code.


if (dataFileLine.Length == NUM_COLUMNS)
{
     if (dataFileLine[10].Trim() == string.Empty
     || dataFileLine[15].Trim() == string.Empty
     || dataFileLine[21].Trim() == string.Empty
     || dataFileLine[32].Trim() == string.Empty)
     continue;
}







加里




Gary

推荐答案

这是代码分析的性能建议...

想法是使用IsNullorEmpty()方法而不是==空。

在你的情况下:

This is a performance suggestion by code analysis...
The idea is to use IsNullorEmpty() method instead of == Empty.
In your case:
if (string.IsNullOrEmpty(dataFileLine[10])
  || string.IsNullOrEmpty(dataFileLine[15])
  || string.IsNullOrEmpty(dataFileLine[21])
  || string.IsNullOrEmpty(dataFileLine[32])



供参考 - http://msdn.microsoft。 com / zh-CN / library / ms182279.aspx [ ^ ]


它告诉您通过检查其长度来检查空字符串,而不是将其与String.Empty的引用进行比较。

It's telling you to check for an empty string by checking its Length, not comparing it to the reference of String.Empty.
if (dataFileLine[10].Trim().Length == 0
    || dataFileList[15].Trim().Length == 0
....



这个小问题就是如果有的话那些dataFileLine []引用返回null,对Trim的调用将失败,并且对象未设置为对象的实例错误。


The tiny little problem with that is if any of those dataFileLine[] references returns null the call to Trim will fail with a "Object not set to an instance of an object" error.


这篇关于我得到CA1820“使用字符串长度测试空字符串”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆