Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Possibly incorrect assignment to local 'variable' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
There are several scenarios where using
or lock
blocks will result in a temporary leak of resources. Here is one example:
thisType f = null;
using (f)
{
f = new thisType();
...
}
In this case, the original value, such as null, of the variable thisType
will be disposed of when the using
block finishes executing, but the thisType
object created inside the block will not be, although it will eventually get garbage collected.
To resolve this error, use the following form:
using (thisType f = new thisType())
{
...
}
In this case, the newly allocated thisType
object will be disposed of.
The following code will generate warning CS0728.
// CS0728.cs
using System;
public class ValidBase : IDisposable
{
public void Dispose() { }
}
public class Logger
{
public static void dummy()
{
ValidBase vb = null;
using (vb)
{
vb = null; // CS0728
}
vb = null;
}
public static void Main() { }
}
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now