简体   繁体   English

为什么可以将空条件运算符应用于硬编码字符串?

[英]Why can I apply a null-conditional operator to a hardcoded string?

I have a bool variable like this: 我有一个bool变量,像这样:

bool myBool = true;

If I write if (myBool == null) I get the following warning: 如果我写if (myBool == null)收到以下警告:

The result of the expression is always 'false' since a value of type 'bool' is never equal to 'null' of type 'bool?'. 表达式的结果始终为“ false”,因为类型“ bool”的值永远不会等于类型“ bool?”的“ null”。

That's clear to me because it doesn't make sense to check whether a non-nullable variable is null. 这对我很清楚,因为检查非可空变量是否为null没有意义。 Visual Studio notices that and marks as warning. Visual Studio注意到这一点并将其标记为警告。

Now I have a string , which is nullable by default, as I know. 现在,我有一个string ,据我所知默认为空。

Why can I apply a null-conditional operator to a hardcoded string without Visual Studio noticing it? 为什么在Visual Studio没有注意到的情况下,我可以将空条件运算符应用于硬编码string I'm thinking of something like this: 我在想这样的事情:

"This is a string"?.AnyStringMethod();

Shouldn't Visual Studio notice that this string isn't null at all? Visual Studio是否不应该注意到此string根本不为null?

Warnings are for code that looks right but is actually wrong . 警告是针对看起来正确但实际上是错误的代码。

Your code looks wrong but does the right thing anyways . 您的代码看起来不对,但无论如何做对了

Therefore: no warning. 因此:无警告。

Visual Studio must go off the type that the operator is working with. Visual Studio必须退出操作员正在使用的类型

When you create a bool , there is no way that it could ever be null by the type, until you change that type to bool? 创建bool ,在将类型更改为bool?之前,无法通过类型将其设置为null bool? .

However, with a hard coded string, even though it has text within the quotes, there's no guarantee that it will stay there. 但是,对于硬编码的字符串,即使它在引号中包含文本,也无法保证它会保留在该位置。 The "variable" that gets created (even as just a plain string) is still of type string , which is able to have a null assigned to it and not change the type. 创建的“变量”(即使只是纯字符串)仍然是string类型,可以为其分配null而不更改类型。

What you are looking for would be for them to inspect the value of every variable that they are creating. 您要寻找的是让他们检查他们正在创建的每个变量的值。 If they were to do that, then why not also check for something like this? 如果他们要这样做,那为什么不还检查类似的东西呢?

var i = 0;

if (i > 2) // This will always be false!

Update 更新资料

As InBetween mentioned in the comments, there is a bit of an oversight in here as well. 正如评论中提到的InBetween一样 ,这里也有一些疏忽之处。 Having a string such as "Some string" that is not assigned in a variable is functionally equivalent to const string s = "Some string"; 没有在变量中分配的诸如"Some string"类的"Some string"在功能上等同于const string s = "Some string"; . If you were to declare it like that, the code inspectors will detect if you run a comparison on that, such as the following: 如果要这样声明,则代码检查器将检测是否对此进行了比较,例如:

const string s = "Some String";
if (s == null) // This will give a warning that this can't happen

I would attribute that difference in the way that the const is handled versus the way a plain static string is handled could be attributed to different development teams working on different parts at different times. 我将这种区别归因于处理const的方式与处理普通静态字符串的方式的不同,这可以归因于在不同时间处理不同部分的不同开发团队。 Again, this is such an edge case that doesn't cause huge problems that it doesn't get warned that no one working on it most likely didn't think about it. 同样,这是一个极端情况,不会引起大问题,因此不会警告说没有人对此进行过思考。

Because no one thought about it? 因为没有人考虑过吗? Your code is so pointless that probably no one foresaw it would ever be used in production code. 您的代码是如此毫无意义,以至于可能没有人预见到它将在生产代码中使用。 I'm pretty sure this scenario didn't even crop up once in the C# design comittees although I'd take that with a grain of salt until someone like Eric Lippert sheds more light on the issue. 我敢肯定,这种情况在C#设计委员会中甚至都没有出现过,尽管我会坚持不懈,直到像Eric Lippert这样的人对这个问题有了更多的了解。

C# sharp isn't born with all potential features and then someone decides to prune it. C#Sharp并非天生具有所有潜在功能,因此有人决定对其进行修剪。 In order for the compiler to give a certain warning someone has to think about it, implement it, test it and document it. 为了使编译器发出一定的警告,必须有人考虑,实施,测试并记录它。

In case of myBool == null , the warning is justified becuase its a plausible error that could potentially make it into production code and its clearly a bug in the program's logic. 如果myBool == null ,则警告是合理的,因为它是一个可能的错误,有可能使它成为生产代码,并且显然是程序逻辑中的错误。 The second scenario is completely harmless even if it ends up making it into production, so the warning really doesn't make much sense. 第二种情况即使最终投入生产也完全没有害处,因此该警告确实没有多大意义。

Because bool is of a value type while string is a reference type 因为布尔是值类型,而字符串是引用类型

Value types cannot be null, but reference types are automaticly nulled through Default 值类型不能为空,但引用类型会通过默认值自动为空

The reason Visual Studio doesn´t notice, is because it is of no importancable really.. it is like asking if blue is more of a color than green Visual Studio不会注意到的原因是,因为它实际上并不重要。这就像询问蓝色是否比绿色更具有颜色

A string literal is slightly different than a string object. 字符串文字与字符串对象略有不同。 I believe the string literal is basically like a constant, it is immutable, and will not ever be null. 我相信字符串文字基本上像一个常量,它是不可变的,并且永远不会为null。

When you assign a string literal to a variable, you are creating a reference to the string in a memory location. 当将字符串文字分配给变量时,将在内存位置中创建对该字符串的引用。 That reference can be null. 该引用可以为空。 If you try to concatenate your string variable with another string and store back in your original string variable, the original string in memory is destroyed and a new string is created that is the concatenated string. 如果您尝试将字符串变量与另一个字符串连接起来并存储回原始字符串变量中,则内存中的原始字符串将被销毁,并创建一个新的字符串作为串联字符串。 This is because strings are always immutable. 这是因为字符串始终是不可变的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM