3 ms·
Stylistic side note for C#: If you're nesting using blocks like that you can leave out the braces in all but the deepest instance which reads a little nicer, im
by ygra 13y ago
Stylistic side note for C#: If you're nesting using blocks like that you can leave out the braces in all but the deepest instance which reads a little nicer, imho:
using (handle a = new handle(0))
using (handle b = new handle(1))
using (handle c = new handle(2)) {
// use a, b, c
}
- detrino 13y agoThanks for that, updated the example.
- dkuntz2 13y agoThat actually looks terrible to me, but I don't program in C#. Mostly it's because there are three blocks, but it only looks like there's one.
- ygra 13y agoIt gets rid of excessive nesting when you need multiple resources allocated after another (e.g. SqlConnection, SqlCommand, etc.). You can do using (handle a = new handle(0), b = new handle(1), c = handle(3)) { // use a, b, c } instead, too, but that obviously only works with equal types. I'm usually not a friend of too deep nesting (worst thing I've seen in our codebase was 65 spaces deep) and in C# you already have one level for the class, one for the namespace (possibly) and another for the method. No need to add two more if you can help it.
- dkuntz2 13y agoRight, I understand that it removes excessive nesting, but the first example just looked ... wrong to me. Your example with above looks somewhat better.