Last active
April 24, 2019 20:27
-
-
Save joncham/94de713da94682a61ec35dbd6f057290 to your computer and use it in GitHub Desktop.
Why do these warnings and errors happen?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.