这段代码工作正常:
policy.ProviderID > 0 ? RefDataSources.LegalBodies.GetDisplayName(policy.ProviderID.Value) : null
Run Code Online (Sandbox Code Playgroud)
但是Resharper抱怨policy.ProviderID.Value需要null检查(以防止InvalidOperationException).
据我所知,条件只会对大于的非空值进行求值,0因此不需要进一步检查.
我应该将此记录为JetBrains的错误吗?或者我误解了什么.
Resharper经常抱怨我显式初始化我的类中的字段虽然它是多余的,因为没有初始化它们将具有相同的(默认)值.但是,我经常更喜欢显式初始化,因为恕我直言,代码变得更清晰,我在阅读代码时必须少思考.
但是,当我只想关闭此检查时,我注意到默认严重性设置为"警告".到目前为止,我一直认为这只是一个机会删除一些代码字符的提示,但现在我想知道在使用冗余初始化程序时是否确实存在风险.
我错过了什么吗?冗余初始化程序会出现哪些错误?
我有以下方法在C#类中包含一个简单的foreach循环.该方法返回使用单独类上的函数计算的总计的总和.
private readonly ICalculateTotalService _calculateTotalService;
public decimal GetTotal(IOrder order)
{
decimal paidTotal = 0;
foreach (var line in order.Lines)
{
paidTotal += _calculateTotalService.GetTotal(line);
}
return paidTotal;
}
Run Code Online (Sandbox Code Playgroud)
Resharper建议将其重构为LINQ语句.这样做最好的方法是什么?
对于我正在开发的新项目,我的团队和我正在尝试使用ReSharper 10,NUnit,SpecFlow和Selenium设置一些功能测试.我们从一个已知的工作项目迁移了一堆旧代码,并使其构建没有问题.
但是,当我们运行代码时,我们不断收到以下错误:
OneTimeSetUp: System.IO.DirectoryNotFoundException : Could not find a part of the path 'C:\Users\Me\AppData\Local\SomeApp\App_Data\SomeApp.db'.
Exception doesn't have a stacktrace
Run Code Online (Sandbox Code Playgroud)
......这不对; 如果它找不到SomeApp.db文件,那应该是失败的C:\MyProjects\SomeApp\SomeApp.Web\App_Data\SomeApp.db!
这种情况下的相关代码在我们的TestSuite.cs类中:
[Binding]
public class TestSuite
{
private string _currentPath;
private string _localFile;
private string _websiteBinPath;
private string _websiteDataPath;
// Stuff...
[BeforeTestRun]
public static void BeforeTestRun()
{
_currentPath = Directory.GetCurrentDirectory() + @"\";
// Data:
// <add key="TestWebsiteDataRelativePath" value="..\..\..\SomeApp.Web\App_Data\" />
// <add key="TestWebsiteBinRelativePath" value="..\..\..\SomeApp.Web\bin\" />
_websiteBinPath = Path.Combine(_currentPath, ConfigurationManager.AppSettings["TestWebsiteBinRelativePath"]);
_websiteDataPath = Path.Combine(_currentPath, ConfigurationManager.AppSettings["TestWebsiteDataRelativePath"]);
//removes the ../../../ …Run Code Online (Sandbox Code Playgroud) 我有一个PushSharp示例中的代码,详细说明了如何处理异常.出于某种原因else,Resharper将所有条件都显示为灰色,并说明了这一点The expression is always false.我不知道这是怎么可能的.
// ex is an Exception passed in to the method
if (ex is NotificationException)
{
// Deal with the failed notification
var notification = ((NotificationException)ex).Notification;
var logItem = new PushLog($"{typePrefix} Notification failed", $"Notification Failed: {notification}");
_pushLogRepo.Insert(logItem);
}
else if (ex is DeviceSubscriptionExpiredException) // Resharper says this is always false
{
// exception handling code...
}
else if (ex is RetryAfterException) // Resharper says this is always false
{
// exception handling …Run Code Online (Sandbox Code Playgroud) 我有条件:
if (dr_dados["DAT_SAIDA"] != null)
{
txtDataSaida.Text = "";
}
else
{
txtDataSaida.Text = dr_dados["DAT_SAIDA"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Jetbrain的ReSharper,它告诉我可以转换成三元操作.
所以,它变成了这样:
txtDataSaida.Text = (dr_dados["DAT_SAIDA"] != null) ? dr_dados["DAT_SAIDA"].ToString() : "";
Run Code Online (Sandbox Code Playgroud)
但后来它告诉我,我可以转换为空合并操作,它给了我这个:
txtDataSaida.Text = dr_dados["DAT_SAIDA"]?.ToString() ?? "";
Run Code Online (Sandbox Code Playgroud)
我有点知道null合并操作的作用,但是有一些不同的东西,我以前没见过的东西,我想知道它是什么.
这个额外的审讯就在这里:
v
txtDataSaida.Text = dr_dados["DAT_SAIDA"]?.ToString() ?? "";
Run Code Online (Sandbox Code Playgroud)
它是什么意思/是什么意思?
c# resharper ternary-operator null-coalescing-operator c#-6.0
我对该类进行了一些更改,它位于某些共享项目中,因此可以在许多网站中使用。如何确定哪些项目会受到影响?
这很有用的情况:我只想更新已更改的生产服务器,或者想了解我的更改有多有害。
当前的解决方案(使用resharper或仅使用visual studio):查找类的用法,然后查找所有用法的用法,然后查找用法的用法等。有什么方法可以使它更容易?
为什么接口成员的C#8默认实现会报告错误?
public interface Logger
{
void Info(string message);
void Error(string message);
// C#8 Default implementations of interface
void Warn(string message)
{
// "interface method cannot declare a body" error message
}
}
Run Code Online (Sandbox Code Playgroud)
和
.NET Core 3.0的配置如屏幕截图所示。
ReSharper 似乎以一种非常简单(太简单)的方式解决了这种情况:
public ActionResult Payment()
{
IOrder order = PaymentBase.GetOrderFromRequest(this.Request.Params); //this method can return null sometimes
int payForm = order.PayForm;
if (order != null)
PaymentService.Check(order, "push", payForm);
else
LogsService.SaveDataLog(WebShopLogType.PaymentInfo, "order missing on push", (object)this.Request.Params.ToString(), "p");
return new EmptyResult();
}
Run Code Online (Sandbox Code Playgroud)
我的空检查order != null标有“表达式始终为真”消息,因此我的整个else语句都被视为没有必要。
我发现这个假设是基于order.PayForm. ReSharper 假设调用对象的PayForm属性order意味着它肯定不为空。删除此行会消除“表达式始终为真”消息。
它显然应该int payForm = order.PayForm;用“可能为空”消息来标记行。
这是一个错误还是我遗漏了什么?
这是一个有趣的:
Resharper希望改变这个:
var lc = new LoginCredentials();
lc.Username = "CesarChavez";
lc.Password = "CesarChavez";
Run Code Online (Sandbox Code Playgroud)
......进入这个:
var lc = new LoginCredentials {Username = "CesarChavez", Password = "CesarChavez"};
Run Code Online (Sandbox Code Playgroud)
......但是对于后者,"lc"似乎是多余的 - 它没有被引用到任何地方...... ???
我希望看看是否有一种简单的方法可以使用Code Resharper或CodeRush或任何其他工具在整个应用程序中使用Convert.ToString()替换().ToString()?
谢谢
Resharper发现1,953个问题与处女的asp.net MVC 3互联网应用程序(我甚至没有触摸代码 - 只看了它):
Common Practices and Code Improvements: 1024 issues
Constraints Violations: 150 issues
Potential Code Quality Issues: 252
Redundancies in Code: 77 issues
Redundancies in Symbol Declarations: 450 issues
Run Code Online (Sandbox Code Playgroud)
一个asp.net新手怎么知道(当他试图重新编写自己的代码时)哪些问题是真正的问题,哪些是非问题?
resharper ×12
c# ×10
.net ×1
c#-4.0 ×1
c#-6.0 ×1
c#-8.0 ×1
directory ×1
evaluation ×1
html ×1
linq ×1
null ×1
nunit ×1
refactoring ×1
unit-testing ×1