Let’s say I want to use 2 classes which implement the IDispose pattern. One of the classes uses the other for instantiation but is not needed afterwards. When stacking “using” keywords this will result in (for example) a locked file for the duration of using the font. I want the file to be unlocked right after I’m done with it for “coding beauty” reasons.
Have a look at the following “tag-like” system.
using1 (Stream stream = File.OpenRead("font.ttf"))
using2 (Font font = FontExtensions.FromStream(stream, 32))
ENDusing1
//use font here
ENDusing2
The hierarchical way, how code blocks are designed, impose a (beauty) limitation on the current handling of this. Ofcourse this can be solved by using try/finally blocks, but these aren’t as neat.
Am I missing a fundamental design with using (lol?) nested IDisposable objects / Streams?
Please share your opinion on tag-code-blocks and the best way to solve this (coding beauty).
You could create a utility method to create your object from a temporary resource:
your example could then be: