C# VS 2010 reports error while editing but it compiles and runs successfully
I have optional parameters generated by CodeDom. For example:
class Squ开发者_Go百科are
{
public Square([Optional()] int side) { }
}
I have a call statement:
Square sq = new Square();
While I'm editing, my Error List window shows: 'Square' does not contain a constructor that takes 0 arguments'
But it compiles and runs successfully. How can I get rid of the error in 'Error List' window? Thanks!
What you're seeing is the difference between IDE live semantic error checking and the actual compiler running. The live semantic checking uses the C# compiler but doesn't have 100% parity with it and it can produce false positives in corner cases of the language.
To get rid of this disable live semantic checking
- Tools -> Options
- Text Editor -> C# -> Advanced
- Uncheck "Show live semantic errors"
Another way to fix this is to use the actual C# supported syntax for optional values
public Square(int side = 0) { }
精彩评论