The code below implements the retry action for a failed background job.
[CanBeNull]
public object Test() { return null; }
public void UseTest()
{
var p = Test();
var s = p.ToString(); // Warning: Possible 'System.NullReferenceException'
}
[NotNull]
public object Foo()
{
return null; // Warning: Possible 'null' assignment
}
[StringFormatMethod("message")]
public void ShowError(string message, params object[] args) { /* do something */ }
public void Foo()
{
ShowError("Failed: {0}"); // Warning: Non-existing argument in format string
}
public void Foo(string param)
{
if (param == null)
throw new ArgumentNullException("par"); // Warning: Cannot resolve symbol
}
public class Foo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void NotifyChanged(string propertyName) { ... }
private string _name;
public string Name {
get { return _name; }
set { _name = value; NotifyChanged("LastName"); /* Warning */ }
}
}
Examples of generated notifications:
Function Definition Table syntax:
[ContractAnnotation("=> halt")]
public void TerminationMethod()
[ContractAnnotation("halt <= condition: false")]
public void Assert(bool condition, string text) // regular assertion method
[ContractAnnotation("s:null => true")]
public bool IsNullOrEmpty(string s) // string.IsNullOrEmpty()
// A method that returns null if the parameter is null, and not null if the parameter is not null
[ContractAnnotation("null => null; notnull => notnull")]
public object Transform(object data)
[ContractAnnotation("s:null=>false; =>true,result:notnull; =>false, result:null")]
public bool TryParse(string s, out Person result)
[LocalizationRequiredAttribute(true)]
public class Foo
{
private string str = "my string"; // Warning: Localizable string
}
[CannotApplyEqualityOperator]
class NoEquality { }
class UsesNoEquality
{
public void Test()
{
var ca1 = new NoEquality();
var ca2 = new NoEquality();
if (ca1 != null) // OK
{
bool condition = ca1 == ca2; // Warning
}
}
}
[BaseTypeRequired(typeof(IComponent)] // Specify requirement
public class ComponentAttribute : Attribute { }
[Component] // ComponentAttribute requires implementing IComponent interface
public class MyComponent : IComponent { }
[Pure] private int Multiply(int x, int y) { return x * y; }
public void Foo() {
const int a = 2, b = 2;
Multiply(a, b); // Waring: Return value of pure method is not used
}
int[] numbers = { 123, 456, 789 };
IEnumerable<int> result = numbers.Pairwise(5, (a, b) => a + b);
The