How can an operator have both = and ==?

I found this other question about files and directories , and the answer included this statement:

bool isDir = (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory; 

His question was about .net, and I encoded in C # .net in Visual Studio. Is it possible to have both an assignment operator and an equality operator in the same operator for all .net languages, or is it specific to certain? Also, can I get an explanation of how this code works? Assuming path refers to a directory, I would expect isDir be true, but can anyone explain why?

+6
source share
6 answers

The equality test performed by the == operator takes precedence over the assignment performed by the = operator. Therefore, the isDir variable will be set to true if both sides of the == operator are equal, otherwise it will be set to false . In other words, this is equivalent to saying:

 if ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory) isDir = true; else isDir = false; 

This is possible in VB.NET. I cannot answer for other languages. In VB.NET, the equivalent would be:

 Dim isDir As Boolean = ((File.GetAttributes(path) And FileAttributes.Directory) = FileAttributes.Directory) 

Since VB uses the same character ( = ) for assignment and equality operators, it determines which operation you perform based on context. The VB compiler is smart enough to know that the first = operator is the destination, and the second the equality test. However, this is clearly confusing, so it is often discouraged for readability. This is especially confusing for people with experience in other languages. For example, in C # you can do the following to set two variables to the same value:

 int y; int x = y = 5; // Both x and y will be set to 5 

The reason that arises in C # is that = always an assignment operator, and the assignment expression always evaluates (returns) the value that was assigned. Therefore, in this case, the expression y = 5 not only assigns the value 5 to the variable y , but also evaluates the value 5. So, when you set the value x to the value of this expression, it is also set to 5. In VB, however, the result is very different:

 Dim y As Integer Dim x As Integer = y = 5 

In VB, the compiler will assume that the expression y = 5 is an equality test, so it will evaluate to false . Therefore, it will try to set x = False , which may or may not work depending on the value of Option Strict .

+13
source

Here's how this code works. The right side evaluates simply true or false, therefore, the equivalence operator (==). This value is then set using the assignment operator (=).

Another example in which both operators are used (albeit in different ways) is in the IO file:

 while((line = streamReader.ReadLine()) != null) { //do file IO } 
+6
source

The value of r is evaluated before the assignment is performed. Rvalue is a comparison of equality. The result of the comparison is the logical value true or false, which, of course, can be assigned to bool.

This is the same as saying

 if((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory) { isDir = true; } else { isDir = false; } 

But more concise.

This is not a special case of appropriation and equality in the same statement. You could do something like

 bool bar, baz, qux; bar = qux = false; baz = true; bool foo = (bar || baz || qux); // foo is true 

foo becomes true if any of the bar , baz or qux is true. You can evaluate any expression in r-value.

+5
source

= is an assignment operator; it sets the value of isDir

== is a comparison operator and checks if the values ​​on both sides are equal

+3
source

== is an operator that returns a boolean in C #. isDir assigned the result of this statement. This is not limited to C #, but is not necessarily common to all .net languages ​​(for example, as far as I know, VB.net still uses single peers to test equality).

+2
source

This statement gets the attributes for the file path (with flag enumeration) ANDing, which is with the enum value for the directory (this essentially erases all bits of the enumeration to 0, except that it represents FileAttributes.Directory . Then it compares the result of this operation to make sure that it is actually equal to the enumeration value representing FileAttributes.Directory result of this operation is then stored in the isDir variable.

This code is equivalent to:

 FileAttributes pathAttributes = File.GetAttributes(path); FileAttributes checkForDirectory = pathAttributes & File.GetAttributes(path); bool isDir = checkForDirectory == FileAttributes.Directory; 

For more information on how flag enumerations work (which is FileAttributes), see Enums

+1
source

Source: https://habr.com/ru/post/916126/


All Articles