Should we move to c# 8 using-declaration?
C# 8 officially got released on 23rd Sep 2019 and It has several new features.
Today we will talk about one of the new feature using-declaration.
As we all know the traditional using statement is making developers life easier for a long time, it automatically disposes of the object without writing a single line of code.
First, let’s take a look into the traditional using statement, let say I have a class which implements the IDisposable interface (as shown in image).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//A class
public class SomeDisposableType : IDisposable
{
...implmentation details...
}
//traditional using statement
using (SomeDisposableType u = new SomeDisposableType())
{
OperateOnType(u);
//u.Dipose is called
}
The using statement can be used to reference a variable or the result from a method, and at the end of the scope Dispose method gets invoked
behinds the scenes, compile creates the code using try/finally (as shown in image).
1
2
3
4
5
6
7
8
9
10
11
12
13
SomeDisposableType t = new SomeDisposableType();
try
{
OperateOnType(t);
}
finally
{
if (t != null)
{
((IDisposable)t).Dispose();
}
}
In c# 8, the code of using statement can be simplified. curly braces are no longer needed, the end of the block will be the scope of the variable and compiler makes try/finally block to make sure the object is disposed.
1
2
3
4
5
6
7
8
9
10
11
public class SomeDisposableType : IDisposable
{
...implmentation details...
}
//c# 8 feature
using var u = new SomeDisposableType();
OperateOnType(u);
if you use nested using statement your code will look like stairs with lots of curly braces, and it’s hard to keep track of scops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class SomeDisposableType : IDisposable
{
...implmentation details...
}
public class SomeOtherDisposableType : IDisposable
{
...implmentation details...
}
using (var u = new SomeDisposableType()){
using (var v = new SomeOtherDisposableType()){
OperateOnType(u);
}
}
let’s do the same with the new using-declaration. The following code is even shorter compared to the previous one — no matter how many resources you need to dispose.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class SomeDisposableType : IDisposable{
...implmentation details...
}
public class SomeOtherDisposableType : IDisposable{
...implmentation details...
}
public void SomeMethod(){
using var u = new SomeDisposableType();
using var v = new SomeOtherDisposableType();
OperateOnType(u);
OperateOnType(v);
//u.dispose
//v.dispose
}
it looks a small feature, but this can make code cleaner and less buggy.
if the method is small, then we should definitely use simplified using and it also helps the compiler, now compiler doest have to keep track of all the scopes.
I think I’ll switch to the new using-declaration with all my code and hope you will do the same.