"for (bool flag = true; flag; flag = false) {...}" is this normal?

I have never seen a for loop initialized this way, and I donโ€™t understand why it would be written this way?

I am doing some research on connecting to an IMAP server in .NET and started looking for code from a library called ImapX . I found the for loop in a method that writes data to NetworkStream and then seems to read the response inside the for funk loop. I do not want to copy and paste the code of another user verbatim, but here the gist:

 public bool SendData(string data) { try { this.imapStreamWriter.Write(data); for (bool flag = true; flag; flag = false) { var s = this.imapStreamReader.ReadLine(); } } catch (Exception) { return false; } return true; } 

Again, this is not exact code, but it is a general idea. What the whole method does is it does not use the server response, it just returns true if no exception has been thrown. I just don't understand how and why the for loop is used this way; can anyone explain what benefits initialize these proposals, if any?

+4
source share
4 answers

I previously suggested that you look at the source code, but from your comments, it sounds like you are looking at MSIL-based Reflector in C #. It is important to understand that this does not necessarily bear close resemblance to the source code as written.

At the MSIL level, there is no for loop or while . There are only conditional branch instructions. (See here: http://weblogs.asp.net/kennykerr/archive/2004/09/23/introduction-to-msil-part-6-common-language-constructs.aspx ). Any tool trying to recreate C # should make a few guesses about how the code was originally created. It seems likely that this was not originally written as a for loop, but this reflector found that IL could have been generated by a for loop, and its heuristic had a poor idea that it should be a for loop, and not something else.

If I look at the same code in ILSpy, it appears as a while . This is still redundant, but looks a lot less strange. In addition, it is possible that the source code actually did what was optimized, perhaps calling the [Conditional] methods or the code marked with the #if directives. Again, perhaps the source code used to execute something other than the parts was commented out - comments were not saved in IL. Or perhaps there was more code in the past and it just left.

In short, there are many ways that you see in Reflector, which can be very different from what was originally written. You should consider this more beautiful view of IL, not the C # example, as it is written by people.

+2
source

This is a terrible way to execute a loop once. If you change the flag initializer to something that is not always true , it may make a little more sense, but not much. I completely-frivolously suggested this code before:

 Animal animal = ...; for (Dog dog = animal as Dog; dog != null; dog = null) { // Use dog... } 

... as a way of using the as operator without "contaminating" the outer area. But this is the stupidity of the language, and not what I have ever really used.

+5
source

I had this as a comment, but I believe this is also the answer.

The "cycle" is meaningless. It initializes flag to true and is declared for execution only when flag is still true . However, flag explicitly set to false after the first iteration. Therefore, in this case, it is guaranteed to run once and only once.

I suspect that the author was trying to make sure that the control flow will be suspended on ReadLine() , but it will still be there until a user login is received.

+1
source

The for-loop loop almost doesn't work, as others have noted.

However, he does one thing that may or may not matter: he enters a namespace scope. The scope of the variable s is the body of the for loop. s goes out of scope after exiting the for loop. You can get the same effect by simply creating a block like this:

 { var s = this.imapStreamReader.ReadLine(); } 

This is still pretty stupid.

I donโ€™t know what the original author tried to do, perhaps trying to ensure that his s destroyed / garbage collected / deleted, and not that this method will work (itโ€™s not).

0
source

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


All Articles