It is not treated different. The compiler does not check if the code-block is doing something usefull. You introduced it, so it probably has a use (take not of post-processing like some code-injecting frameworks).
It merely checks whether each block has a correct flow. What use would it serve to also see if your code-block does throw exceptions, simply to check whether the exception handler "might" fire? Would make compiling a lot slower, since it would be checking a lot.
Now what is the upside of this check? To not run into the warning?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
var filename = "wwwroot/Counter/Counter.txt";
var counterStream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
var reader = new StreamReader(counterStream);
var visits = Convert.ToInt32(reader.ReadLine());
visits = visits + 1;
var writer = new StreamWriter(counterStream);
As you will realise, it is a counter for a website I am building. I am OK down to the line.
visits = visits + 1
Visits does contain one more than is originally stored in Counter.txt. However, the last three lines are not writing anything back to the file. I was expecting it to write a new line in the file, although obviously I want it to replace the original finally. I am at a loss as to why it hasn't. Could someone point me in the right direction, please.
You need to position the file pointer to the appropriate place, either by reading all of the existing lines (and keeping only the last one), or by using Stream.Seek to reposition the file pointer back to the beginning before the Write.
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
Counter.txt only contains the one number, the number of visits the site has had. My code reads and updates it OK but the code after that doesn't write it back to replace the one that is there. That's all I want to do; replace the old with the new value.
Have you read and realized what Midi_Mick has written to you ?
You have to do the following to get the saved Counter :
1.1. Open the File
1.2. Read your Value
1.3. Close the File
And you have to do the following to save the Counter :
2.1. Open the File
2.2. Write your Value
2.3. Close the File
The problem as I see it is that closing the file half way through the update opens it up for another visitor to start updating it before I have written the updated value from the first visit. This negates the point of having a counter.
I thought it would save me typing a similar explanation again, although I suppose I could have cut and pasted most of it and edited it where necessary. Anyway, thank you for taking the time to answer my question. It is now working as required.
You want to drag a picture based on the mouse movement? Draw a picture. Work out what your mouse offset is in that picture. Whenever you move your mouse, reposition the picture taking that offset into account. There, you have everything you need to know to write this for yourself.