Since the C# using statement is just a syntactic sugar for try/finally{dispose} why does it accept multiple objects ONLY IF THEY ARE OF THE SAME TYPE?
I don't get it since all they need to be is IDisposable. If all of them implement IDisposable it should be fine but it isn't.
Specifically I am used to writing
using (var cmd = new SqlCommand())
{
using (cmd.Connection)
{
// code
}
}
which I compact into:
using (var cmd = new SqlCommand())
using (cmd.Connection)
{
// code
}
and I would like to compact furthermore into:
using(var cmd = new SqlCommand(), var con = cmd.Connection)
{
// code
}
but I can't. I could probably, some would say, write :
using((var cmd = new SqlCommand()).Connection)
{
// code
}
since all I need to dispose is the connection and not the command but that's besides the point.