有人能建议如何从任何单行语句中移除大括号吗?(不包括显而易见的,只需手动删除大括号)
在中使用C#。
因此,与其:
if (thingy is null)
{
throw new ArgumentNullException(nameof(thingy));
}可供选择:
if (thingy is null)
throw new ArgumentNullException(nameof(thingy));我尝试过运行CodeMaid并更改CodeCleanup (这只是将其更改为有大括号)。我很高兴尝试任何推荐的扩展等等来解决这个问题。
发布于 2020-01-15 12:50:34
这不是Visual中的标准重构。但也有一些扩展添加了这一点。
例如:Roslynator有其移除扶手重构。
发布于 2021-04-19 18:00:04
如果您使用的是VisualStudio2019预览,则可以通过两个简单步骤实现您的需要。


发布于 2021-07-30 23:03:37
作为一种习惯,您不应该在单行条件下省略大括号。对于某个人(你或另一个人)来说,很容易对它犯一个小错误,从而造成一个你以后必须处理的错误。
现在,我将离开我的肥皂盒,分享一个更短的空守卫:
public void MyFunction(object thingy)
{
_ = thingy ?? throw new ArgumentNullException(nameof(thingy));
etc...很好,简洁,没有丢失支撑问题的风险。对于字符串,我将使用扩展方法来获得相同的一行。
public static string NullIfWhiteSpace(this string s)
{
return string.IsNullOrWhiteSpace(s) ? null : s;
}那我就能做到:
public void MyFunction(string stringy)
{
_ = stringy.NullIfWhiteSpace() ?? throw new ArgumentNullException(nameof(stringy));
etc...对于空列表和字典,我会做一些类似的事情。
https://stackoverflow.com/questions/59751557
复制相似问题