using declarations were just introduced in C# 8.0 but they don't behave the same as using blocks, or so i think.
The following nested using block works fine:
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(serviceKey))
using (var file = new FileStream(path, FileMode.Create, FileAccess.Write))
{
resource?.CopyTo(file);
}
But when i convert to a using declaration as follows, i get an IOException which says the file is being used by another process:
using var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(serviceKey);
using var file = new FileStream(path, FileMode.Create, FileAccess.Write);
resource?.CopyTo(file);
I want to understand what's different and how\when to use the new using declaration?
IDisposableand you do not plan on disposing it yourself.pathis actually open in another process? Word and Excel are very good at setting exclusive locks on files. It could even be your own process if you are calling both statement blocks within Tasks or async.usingstatement runs ok. It only throws and exception when i use a declaration instead.