<> 而不是在 VB.NET 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/649951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:08:05  来源:igfitidea点击:

<> And Not In VB.NET

vb.netoperatorsquery-string

提问by Kezzer

I'm having the exciting task of finding out about VB.NET's <>and Notoperators. Not- I'm assuming by my small use of it - is the functional equivalent of !in languages such as C# and <>being equivalent of !=.

我有一项激动人心的任务,就是要了解 VB.NET<>Not运算符。 Not- 我假设我很少使用它 - 在功能上相当于!C# 等语言,<>相当于!=.

In VB.NET a common problem is doing Boolean expressions against objects that don't have a reference, it appears. So if we do

在 VB.NET 中,一个常见问题是对没有引用的对象执行布尔表达式,它出现了。所以如果我们这样做

If Request.QueryString("MyQueryString") <> Nothing Then

This will actually fail if the query string doesn't exist. Why, I don't know. The way that it's done by older coders is as follows:

如果查询字符串不存在,这实际上会失败。为什么,我不知道。较老的编码器完成它的方式如下:

If Not Request.QueryString("MyQueryString") Is Nothing Then

And this tends to work. To me they're functionally equivalent though operators tend to do different comparisons dependent on certain factors such as operator precedence, why it doesn't work in this case however, I do not know, and neither have I found any relevant material.

这往往会奏效。对我来说,它们在功能上是等效的,尽管运算符倾向于根据某些因素(例如运算符优先级)进行不同的比较,但是为什么它在这种情况下不起作用,我不知道,我也没有找到任何相关材料。

I ask this as I'm having to write standards documentation and we're determining the use of either the Notor <>. Any ideas on which way around it should be, or you shoulddo it?

我问这个是因为我必须编写标准文档并且我们正在确定使用Not<>. 关于它应该采取哪种方式的任何想法,或者你应该这样做?

回答by Charlie

I have always used the following:

我一直使用以下方法:

If Request.QueryString("MyQueryString") IsNot Nothing Then

But only because syntactically it reads better.

但仅仅是因为它在语法上读起来更好。

When testing for a valid QueryString entry I also use the following:

在测试有效的 QueryString 条目时,我还使用以下内容:

If Not String.IsNullOrEmpty(Request.QueryString("MyQueryString")) Then

These are just the methods I have always used so I could not justify their usage other than they make the most sense to me when reading back code.

这些只是我一直使用的方法,所以除了在读回代码时对我来说最有意义之外,我无法证明它们的使用是合理的。

回答by Rowland Shaw

Isis not the same as =-- Iscompares the references, whilst =will compare the values.

Is=--Is比较引用不同,而=将比较值。

If you're using v2 of the .Net Framework (or later), there is the IsNotoperator which will do the right thing, and read more naturally.

如果您使用的是 .Net Framework 的 v2(或更高版本),那么IsNot操作符会做正确的事情,并且阅读起来更自然。

回答by Cerebrus

I think your question boils down to "the difference between (Isand =) and also (IsNotand <>)".

我认为您的问题归结为“(Is=)与(IsNot和<>)之间的区别”。

The answer in both cases is the same :

这两种情况的答案是相同的:

=and <>are implicitly defined for value types and you can explicitly define them for your types.

=并且<>是为值类型隐式定义的,您可以为您的类型显式定义它们。

Isand IsNotare designed for comparisons between reference types to check if the two references refer to the same object.

Is并且IsNot设计用于引用类型之间的比较,以检查两个引用是否引用同一个对象。

In your example, you are comparing a string object to Nothing (Null) and since the =/<>operators are defined for strings, the first example works. However, it does not work when a Null is encountered because Strings are reference types and can be Null. The better way (as you guessed) is the latter version using Is/IsNot.

在您的示例中,您将字符串对象与 Nothing(空)进行比较,并且由于=/<>运算符是为字符串定义的,因此第一个示例有效。但是,当遇到 Null 时它不起作用,因为字符串是引用类型并且可以为 Null。更好的方法(如您所料)是使用Is/的后一个版本IsNot

回答by Noldorin

Here's the technical answer (expanding on Rowland Shaw's answer).

这是技术答案(扩展 Rowland Shaw 的答案)。

The Iskeyword checks whether the two operands are references to the same object memory, and only returns true if this is the case. I believe it is functionally equivalent to Object.ReferenceEquals. The IsNotkeyword is simply shorthand syntax for writing Not ... Is ..., nothing more.

关键字检查两个操作数是否是同一个对象的内存引用,如果是这样的情况下,只有返回true。我相信它在功能上等同于Object.ReferenceEquals。该IsNot运算关键字是写作语法速记Not ... Is ...,仅此而已。

The =(equality) operator compares values and in this case (as in many others) is equivalent to String.Equals. Now, the <>(inequality) operator does not quite have the same analogy as the Isand IsNotkeywords, since it can be overriden separately from the =operator depending on the class. I believe the case should always be that it returns the logical inverse of the =operator (and certainly is in the case of String), and just allows for a more efficient comparison to made when testing for inequality rather than equality.

=(equality)运算符比较值,并在这种情况下(如在许多其他的)等价于String.Equals。现在,<>(不等式)运算符与IsIsNot关键字的类比并不完全相同,因为它可以根据类与=运算符分开覆盖。我相信情况应该始终是它返回=运算符的逻辑逆(当然是在 String 的情况下),并且只允许在测试不平等而不是相等时进行更有效的比较。

When dealing with strings, unless you actually mean to compare references, always use the =operator (or String.Equals I suppose). In your case, because you are testing for null (Nothing), it seems you need to use the Isor IsNotkeyword (the equality operator will fail because it can't compare the values of null objects). Syntactically, the IsNotkeyword is a bit nicer here, so go with that.

处理字符串时,除非您真的想比较引用,否则请始终使用=运算符(或 String.Equals 我想)。在您的情况下,因为您正在测试 null ( Nothing),所以您似乎需要使用IsIsNot关键字(相等运算符将失败,因为它无法比较 null 对象的值)。从语法上讲,这里的IsNot关键字更好一些,所以就这样吧。

回答by hometoast

If you need to know if the variable exists use Is/IsNot Nothing.

如果您需要知道变量是否存在,请使用 Is/IsNot Nothing。

Using <> requires that the variable you're evaluating have the "<>" operator defined. Check out

使用 <> 要求您评估的变量定义了“<>”运算符。查看

 Dim b As HttpContext
 If b <> Nothing Then
    ...
 End If

and the resultant error

以及由此产生的错误

Error   1   Operator '<>' is not defined for types 'System.Web.HttpContext' and 'System.Web.HttpContext'.   

回答by Christian Hayter

The C# and VB.NET compilers often generate different IL for operations that are apparently equivalent in both languages. It just so happens that C# does the "expected" thing when you write stringvar == null, but VB.NET does not. To get the same effect in VB.NET you have to force true reference equality with the Isoperator.

C# 和 VB.NET 编译器通常为两种语言中明显等效的操作生成不同的 IL。碰巧 C# 会在您编写时执行“预期”的事情stringvar == null,但 VB.NET 没有。要在 VB.NET 中获得相同的效果,您必须强制使用Is运算符进行真正的引用相等。

回答by Travis Heeter

I'm a total noob, I came here to figure out VB's 'not equal to' syntax, so I figured I'd throw it in here in case someone else needed it:

我是个菜鸟,我来这里是为了弄清楚 VB 的“不等于”语法,所以我想我会把它扔在这里以防其他人需要它:

<%If Not boolean_variable%>Do this if boolean_variable is false<%End If%>

回答by Hzj_jie

in fact the Is is really good, since to the developpers, you may want to override the operator ==, to compare with the value. say you have a class A, operator == of A is to compare some of the field of A to the parameter. then you will be in trouble in c# to verify whether the object of A is null with following code,

实际上 Is 真的很好,因为对于开发人员来说,您可能想要覆盖运算符 ==,以与值进行比较。假设你有一个A类,A的运算符==就是将A的一些字段与参数进行比较。那么你在c#中用下面的代码验证A的对象是否为空就会有麻烦了,

    A a = new A();
...
    if (a != null)
it will totally wrong, you always need to use if((object)a != null)
but in vb.net you cannot write in this way, you always need to write
    if not a is nothing then
or
    if a isnot nothing then

which just as Christian said, vb.net does not 'expected' anything.

正如 Christian 所说,vb.net 没有“期望”任何东西。

回答by luiscubal

Just use which sounds better. I'd use the first approach, though, because it seems to have fewer operations.

只使用听起来更好的。不过,我会使用第一种方法,因为它的操作似乎较少。