我可以删除“冗余初始化”吗? Resharper在C#中建议? [英] Can I remove the "redundant initialization" suggested by Resharper in C#?

查看:355
本文介绍了我可以删除“冗余初始化”吗? Resharper在C#中建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我刚接触Resharper工具,所以只是不知道它是如何得出某些建议的。

所以,例如,我宣布一个字符串变量,将其初始化为:



  string  str =  string  .empty;  //  这里我得到删除冗余初始化程序 

if true
{
str = True;
}
else
{
str = false ;
}





我初始化的原因



 str =  string  .empty; 





是因为我不希望编译器分配任何dum我的/垃圾值到变量。



请指导。

提前谢谢



现在根据Abhinav的评论,我改进了下面的问题:

但是,让我们假设,例如,如果我只有


  string  str; 
if true
{
}
else
{
str = ;
}



str中的值是什么?

解决方案

让我们了解冗余初始化首先。



说明

不使用此变量的初始值。初始化之后,变量被赋予另一个值或超出范围。

示例:以下代码摘录分配给变量r,然后覆盖该值而不使用它。

 int r = getNum(); //冗余
r = getNewNum(buf);





在您的情况下,代码运行后, str 将是 True False

所以你不需要担心垃圾值。



只需删除 string str = string.empty; 并将其设为 string str;


要回答修正问题的第二部分,因为TheRealSteveJudge指出了 str <的值/ code>将为null。



更重要的是,如果您在此之后尝试使用 str 你会得到一个错误

Quote:

错误2使用未分配的局部变量'str'

和resharper不会告诉你一个字符串str = string.empty 是多余的。

基本上你应该确保 str 已初始化或确保所有代码路径都导致将值分配给 str



Visual Studio和Resharper都在帮助您删除不必要的代码,并帮助您在尝试运行程序之前找到潜在的错误。


Hi,
Well I m new to Resharper tool, so just unaware how it derives upto certain suggestions.
So, for example, I declare a string variable and initialize it as :

string str=string.empty;// here I get the "Remove redundant initializer

if (true)
{
str="True";
}
else
{
str="false";
}



The reason I m initializing

str=string.empty;



is that because I don't want the compiler to assign any dummy/garbage values to the variable.

Please guide.
Thanks in advance

Now based on Abhinav's comment, I have improved the question with below:
But let us suppose, for instance,if I had only

string str;
if (true)
{
}
else
{
str="false";
}


what value would then be in str??

解决方案

Lets understand Redundant Initialization first.

EXPLANATION
This variable's initial value is not used. After initialization, the variable is either assigned another value or goes out of scope.
Example: The following code excerpt assigns to the variable r and then overwrites the value without using it.

int r = getNum();  //Redundant
r = getNewNum(buf);



In your case, after the code runs, the value of str will be either True or False.
So you don't need to worry about garbage values.

Just remove string str=string.empty; and make it string str;


To answer the 2nd part of your amended question, as TheRealSteveJudge has pointed out the value of str would be null.

More importantly if you then tried to use str after this point you would get an error

Quote:

Error 2 Use of unassigned local variable 'str'

and resharper would not be telling you that a string str = string.empty is redundant.
Basically you should EITHER ensure that str is initialised OR ensure that all the code paths result in a value being assigned to str

Both Visual Studio and Resharper are helping you to both remove code that isn't necessary, and also helping you to find potential errors BEFORE you try to run your program.


这篇关于我可以删除“冗余初始化”吗? Resharper在C#中建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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