Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Based on the code you provided, the error you’re encountering is due to the incorrect usage of type arguments when instantiating the ColorSpecification class. In your code, you’re using ColorSpecification, which suggests that ColorSpecification is a generic class that takes a type argument.

However, in your class definition of ColorSpecification, it is not defined as a generic class. Instead, it directly implements the ISpecification interface. Therefore, you should instantiate ColorSpecification without specifying any type arguments. Here’s the corrected code:

var bf = new BetterFilter();
var cs = new ColorSpecification(Color.Green); // Remove "<Product>"
foreach (var p in bf.Filter(products, cs))
{
    Console.WriteLine($"Green products(new) {p.Name}");
}

By removing the type argument in the instantiation of ColorSpecification, the error should be resolved.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading