Any advantage of using the crop in this case?

I support some code at work, and the author of the original disappeared, so I thought that I would ask here to see if I could satisfy my curiosity.

Below is a code snippet (anonymous) that uses revenue. As far as I can tell, this does not add any benefit, and just returning the list would be sufficient, perhaps more readable (at least for me). It’s just interesting if I’m missing something because this pattern is repeated in several places in the code base.

public virtual IEnumerable<string> ValidProductTypes
{
  get
  {
    yield return ProductTypes.Type1;
    yield return ProductTypes.Type2;
    yield return ProductTypes.Type3;
  }
}

This property is used as a parameter for some class that simply uses it to populate the collection:

var productManager = new ProductManager(ValidProductTypes);

public ProductManager(IEnumerable<string> validProductTypes)
{
  var myFilteredList = GetFilteredTypes(validProductTypes);
}

public ObservableCollection<ValidType> GetFilteredTypes(IEnumerable<string> validProductTypes)
{
  var filteredList = validProductTypes
                    .Where(type => TypeIsValid); //TypeIsValid returns a ValidType
  return new ObservableCollection<ValidType>(filteredList);
}
+4
source share
1 answer

, IEnumerable<T> , yield return, .

, IEnumerable<T>, : . (, LINQ) - .

, , , API. , , ValidProductTypes.Add("a new product")?

, . IEnumerable<T> . , - , API, , yield return , .

, , ​​ , , yield return - . # 6.0 :

public virtual IEnumerable<string> ValidProductTypes =>
  new[] { ProductTypes.Type1, ProductTypes.Type2, ProductTypes.Type3 };

, , . , , . (, - ), .

+5

Source: https://habr.com/ru/post/1685336/


All Articles