Reputation: 14253
I have a class like this:
public class Article {
private Category? category;
private string content;
public Article(Category? category,string content){
Contract.Ensures(this.category == category); // error
}
}
but on Ensure
method this error occurs:
Operator '==' cannot be applied to operands of type 'Category?' and 'Category?'
How can I avoid that?
Upvotes: 2
Views: 9640
Reputation: 6924
first please implement Equals inside Category class
public struct Category // turned out that Category is a struct
{
public int Field {get; set; } // for demo purpose only
public override bool Equals(Object that)
{
if (that == null)
{
return false;
}
else
{
if (that is Category)
{
// select the fields you want to compare between the 2 objects
return this.Field == (that as Category).Field;
}
return false;
}
}
}
then in your code you can use equals method
Contract.Ensures(this.category.Equals(category))
Upvotes: 0
Reputation: 203850
You'll need to overload the ==
operator for that type if you expect to be able to use it to compare two instances (whether nullable or not) of that type.
Upvotes: 5