Skip to content

Instantly share code, notes, and snippets.

@joncham
Last active April 24, 2019 20:27
Show Gist options
  • Save joncham/94de713da94682a61ec35dbd6f057290 to your computer and use it in GitHub Desktop.
Save joncham/94de713da94682a61ec35dbd6f057290 to your computer and use it in GitHub Desktop.
Why do these warnings and errors happen?
// case 1
public struct A : System.IDisposable
{
public void Test()
{
var a = new A();
using (a)
a[0] = 0; // warning CS0728: Possibly incorrect assignment to local 'a' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
}
public int this[int index]
{
get {return 0;}
set {}
}
public void Dispose() {}
}
// case 2
public struct B : System.IDisposable
{
public void Test()
{
using (var b = new B())
b[0] = 0; // error CS1654: Cannot modify members of 'b' because it is a 'using variable'
}
public int this[int index]
{
get {return 0;}
set {}
}
public void Dispose() {}
}
@joncham
Copy link
Author

joncham commented Apr 24, 2019

foo.cs(9,4): warning CS0728: Possibly incorrect assignment to local 'b' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment