Using statement parity with block #2593
Replies: 19 comments 1 reply
-
Just use a discard: using _ = GetSomethingDisposable(); |
Beta Was this translation helpful? Give feedback.
-
This has came up before on the original issue. This would be useful for situations like setting up logging. The answer then was also to use discards. However, as mentioned there, littering the code with In one of the codebases in my company, we also use I don't know if I'm missing something, but in the end I see the original feature being mostly useful for the proverbial, ever-repeated example: public void EveryoneUsesAdoNetNow()
{
using (var connection = new SqlConnection(connectionString))
using (var command = connection.CreateCommand())
{
// setup
using (var reader = command.ExecuteReader())
// do stuff
}
} ... (which is not a lot)... I think there are many other valid use-cases that could benefit from the feature if this was allowed. |
Beta Was this translation helpful? Give feedback.
-
That is literally what discards are for. The reason for using discards, rather than just allowing the value to vanish into the ether is because the latter case is a source of some hard-to-find bugs. Ideally, this would be retrofitted to method calls, so that for example, stream.Read(buffer, offset, bytesToRead); would be a compilation error. But that would be a breaking change. So we have to rely on analyzers to report those instances instead. However new features, like using statements, can be protected in this fashion from the outset.
|
Beta Was this translation helpful? Give feedback.
-
I strongly disagree that using discards is fine. IMO code looks much much better with proposed change and shows the intent clearly. You don't force people to use discards with normal method invocation, it should not be required in this case either. |
Beta Was this translation helpful? Give feedback.
-
@ghord, thankfully the language team disagrees with you. |
Beta Was this translation helpful? Give feedback.
-
The issue is that in the case of method, you can simply return void if return value is not necessary, but there is no "void disposable" way to write a method (i.e. consumer should not care about the result of method, only that it runs some code at the end of scope). |
Beta Was this translation helpful? Give feedback.
-
Discards don't work either:
|
Beta Was this translation helpful? Give feedback.
-
Okay, that looks like a bug. |
Beta Was this translation helpful? Give feedback.
-
Yes, I agree with @Joe4evr that that looks like a bug as I'd expect that to work. |
Beta Was this translation helpful? Give feedback.
-
Definitely, since You're already explicitly stating that you're interested in what the method returns by having the What is in fact dangerous is just doing |
Beta Was this translation helpful? Give feedback.
-
I don't think there's anything wrong. As I said, there is a codebase in our company where we use For background: this code is from a safety-critical system, where a bug could affect the lives of thousands of people. Thus, we put as many logging info as we can so that, when a bug pops up, we take as little time as possible to figure out what caused it. And so it's not like we can -- like someone suggested in the original issue -- just "delete everything". We've also did an analysis on how this new feature would impact our code and we were happy to see that we would be able to replace a lot of the
Although I agree the language would be better off forcing people into do something about return values, I think using that as basis to not implement this is not valid for four reasons:
|
Beta Was this translation helpful? Give feedback.
-
To add to this, if I were a C# programmer who wasn't keeping up to date on the intricacies of the new C# language changes and saw
Neither of those are great outcomes, and they're pretty easily avoidable by simply not imposing the limitation. |
Beta Was this translation helpful? Give feedback.
-
so, any official statements about the bug that that's quite frustrating |
Beta Was this translation helpful? Give feedback.
-
I'd still argue that |
Beta Was this translation helpful? Give feedback.
-
I agree, but I'm talking about something that should be considered a bug and not a design choice because the legacy using statement allows it. |
Beta Was this translation helpful? Give feedback.
-
I'd appreciate an official statement either way, ultimately the problem is that a using block takes expressions (which naturally includes discard assignment expressions) but a using statement does not. I can imagine there could be a parsing problem having to deal with the very similarly-looking |
Beta Was this translation helpful? Give feedback.
-
I don't think that's the problem because using pattern happens within methods and AFAIK you can't declare a namespace inside a method, in any case I don't expect MS guys could be stopped by this :) |
Beta Was this translation helpful? Give feedback.
-
Closing as duplicate of championed issue #2235 |
Beta Was this translation helpful? Give feedback.
-
My bad - this isn't an exact duplicate. Moving to discussion. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Related to #114 #1174
I can write a
using
block with a variable in it:Or no variable if I never access it other than the hidden Dispose call:
In the current C# 8 preview, I can also write a
using
statement with a variable:However I cannot omit the variable if I don't need it:
I'd like to request making the last example valid.
Beta Was this translation helpful? Give feedback.
All reactions