samedi 10 juin 2023

Color Specification class in c# is giving me error : The non-generic type-or-method 'identifier' cannot be used with type arguments

i am learning design patterns in c# language, so i wrote class which filters the products by color(enum). I used interfaces Ispecification and Ifilter which take T as generic type. here is the code i wrote


public interface ISpecification<T>
        {
            public bool IsSatisfied(T t);
        }

        public interface IFilter<T>
        {
            IEnumerable<T> Filter(IEnumerable<T> items, ISpecification<T> specification);
        }

        public class ColorSpecification : ISpecification<Product>
        {
            private Color color;
            public ColorSpecification(Color color)
            {
                this.color = color;
            }

            public bool IsSatisfied(Product product)
            {
                return product.Color == color;
            }
        }

        /// <summary>
        /// new way, implements open close design pattern
        /// </summary>
        public class BetterFilter : IFilter<Product>
        {
            public IEnumerable<Product> Filter(IEnumerable<Product> items, ISpecification<Product> specification)
            {
                foreach (var i in items)
                {
                    if (specification.IsSatisfied(i)) yield return i;
                }
            }
        }
        
    public static void Main(string[] args)
           {    
             var bf = new BetterFilter();
            var cs = new ColorSpecification<Product>(Color.Green);
            foreach (var p in bf.Filter(products, cs))
            {
                Console.WriteLine($"Green products(new) {p.Name}");
            }
         }

when i instantiate a new class for ColorSpecification which determines whether an item of type <Product> satisfies the specification, i am getting an error in line

var cs = new ColorSpecification<Product>(Color.Green);

"The non-generic type-or-method 'ColorSpecification ' cannot be used with type arguments. how can i solve this error?

Aucun commentaire:

Enregistrer un commentaire