“字符串"与“字符串"关于 IsNullOrWhiteSpace() 的快速提示 [英] "String" vs. "string" in quicktip about IsNullOrWhiteSpace()

查看:28
本文介绍了“字符串"与“字符串"关于 IsNullOrWhiteSpace() 的快速提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 2015 中工作,我有条件检查:

if(String.IsNullOrWhiteSpace(stringToTest))

我看到一个 IDE001

<小时>

MSDN 示例 使用带有 String 的大写 S,并且this SO answer 阐明了string 是 C# 中 System.String 的别名.所以从技术上讲,没有区别."

需要明确的是,我的问题依赖于字符串与字符串中的答案,但我有一个不同的问题比那里问的要多.

同样相关的是这个问题,尽管那里的答案并没有真正解决这个问题.该特定问题与我的非常相似,但它被标记为我注意到的另一个 SO 问题的副本.并且OP 有评论表明这是 2015 年才出现的全新行为.

<小时>

我的问题

我的问题是,如果两个变量类型是等价的,并且 MS 示例使用大写版本,为什么我看到使用小写版本的快速操作?.NET 4.6 框架和 VS2015 鼓励使用小写版本吗?我似乎不应该看到这种提示.

解决方案

嗯,比我聪明的人注意到编译级别实际上没有区别,就像你一样(就像你会看到的 JohnyL ;),我也认为这是一个错误并找到了导致我回答的原因:

<块引用>

为什么我会看到使用小写版本的快速操作?

取自 this 内容丰富(且有趣)的错误讨论,这些是此功能的要点:

  • 它不只是改变字母大小写,它用 string 关键字替换了 String 类型名称.2 碰巧仅因情况而异的事实是巧合.存在字符数不同(Int32 -> int)或名称完全不同(Single -> float)的情况.
  • 小写名称更容易输入.
  • 对于那些实际上更喜欢代码中 string 一致格式的人(这可能取决于您编码的其他语言及其约定),此功能有助于将现有源代码更改为一致.
  • string 也是一个具有明确含义的关键字,而 String 的含义可能因上下文而异.
<块引用>

.NET 4.6 框架和 VS2015 是否有更改以鼓励使用小写版本?

据我所知,没有.

顺便说一句,您可以在工具">选项">文本编辑器">C#">代码样式"-> 取消选中在成员访问表达式中首选内在预定义类型关键字"中更改此行为以适合您的偏好.

Working within Visual Studio 2015, I have a conditional check to the effect of:

if(String.IsNullOrWhiteSpace(stringToTest))

And I saw an IDE001 quick tip or action suggesting that the "Name can be simplified" with a suggested correction of:

if(string.IsNullOrWhiteSpace(stringToTest))

With the only difference being to use string instead of String.


MSDN examples use an uppercase S with String, and this SO answer clarifies that "string is an alias in C# for System.String. So technically, there is no difference."

And to be clear, my question relies upon the answers within String vs. string, but I have a different question than what is asked there.

Also related is this SO question, although the answers there don't really address the question. That particular question is very similar to mine, however it is marked as a duplicate of the other SO question I noted. And there is a comment by the OP indicating this is brand new behavior only seen in 2015.


My Question

My question is that if the two variable types are equivalent, and MS examples use the upper case version, why am I seeing quick actions to use the lower case version? Was there a change in the .NET 4.6 framework and VS2015 to encourage using the lower case version? It doesn't seem like I should be seeing that type of a tip.

解决方案

Well, as smarter than me have noted there's actually no difference in the compiling level, and like you (and like JohnyL as you'll see ;), I also thought it's a bug and got to what's leading me to my answer:

why am I seeing quick actions to use the lower case version?

Taken from this informative (and funny) bug discussion, these are the main points for this feature:

  • It doesn't just change the letter case, it replaces the String type name with the string keyword. The fact that the 2 happen to differ only by case is a coincidence. There are cases where the number of characters is different (Int32 -> int) or the name is completely different (Single -> float).
  • Lower case names are easier to type.
  • For people that actually prefer the consistent format of string in the code (it's probably dependent on other languages you code in and their conventions) this feature helps change existing source code to be consistent.
  • string is also a keyword with a well defined meaning while String's meaning may be different depending on context.

Was there a change in the .NET 4.6 framework and VS2015 to encourage using the lower case version?

As far as I've read, No.

BTW, you can change this behavior to suit your preference in Tools > Options > Text Editor > C# > Code Style -> Uncheck "Prefer intrinsic predefined type keyword in member access expressions".

这篇关于“字符串"与“字符串"关于 IsNullOrWhiteSpace() 的快速提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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