Search code examples
c#.netcode-contracts

Operator '==' cannot be applied to operands of type 'Type?' and 'Type?'


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?


Solution

  • 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.