Home > database >  SonarQube: Add a way to break out of this methods recursion C#
SonarQube: Add a way to break out of this methods recursion C#

Time:10-07

We recently started to use SonarQube in our projects. So, when I build the project it gives out a critical error: "Add a way to break out of this method's recursion". here is the code

private Bool SetProperty<T>(T storageField, T value, [CallerMemberName] string pName= "" )
  {
    return SetProperty<T>(storageField, value, pName);
  }

Do you guys know how to fix this in a way that SonarQube won't complain?

CodePudding user response:

"Add a way to break out of this method's recursion"

As already mentioned, you need to break out of that routine, it's calling itself over and over. It appears you're trying to implement the INotifyPropertyChanged interface, assuming because you've tagged wpf.

Try changing your routine as below:

protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propName = "")
{
   if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
      storage = value;
      OnPropertyChanged(propName);
      return true;
}

In the above routine, I am checking that the value is different than the storage, if it's the same, we don't need to go any further and hence we can return false, otherwise we can do our PropertyChanged and return true.

CodePudding user response:

I don't know SonarQube but your method is calling itself :(

  • Related