Cannot create an instance of the variable type 'Item' because it does not have the new() constraint

后端 未结 3 1953
北恋
北恋 2020-12-24 05:08

I am trying to test a method - and getting an error:

Cannot create an instance of the variable type \'Item\' because it does not have the new() constr

相关标签:
3条回答
  • 2020-12-24 05:25

    The Item in the line:

    Item i = new Item(p, 10);
    

    refers to the generic type parameter Item of the CountTestHelper method, not the class Item. Change the generic parameter name e.g.

    public void CountTestHelper<TItem>() where TItem : IHasRect
    {
        Rectangle rectangle = new Rectangle(0, 0, 100, 100); 
        SomeClass<TItem> target = new SomeClass<TItem>(rectangle);            
        Point p = new Point(10,10);
        Item i = new Item(p, 10);    
        ...
    }
    

    alternatively you can fully qualify the name of the Item class you want to create:

    public void CountTestHelper<Item>() where Item : IHasRect
    {
        Rectangle rectangle = new Rectangle(0, 0, 100, 100); 
        SomeClass<Item> target = new SomeClass<Item>(rectangle);            
        Point p = new Point(10,10);
        SomeNamespace.Item i = new SomeNamespace.Item(p, 10);  
    }
    
    0 讨论(0)
  • 2020-12-24 05:35

    Because many people get here by the question tilte (which is very generic and matches the compiler message) let me give a more detailed answer about the compiling error itsef.

    You are using generics in a method. The compiler doesn´t know which type it will receive and thus it is not warranted that your type has a parameterless construtor. For ex:

    class A {
        A(int i){ ... }
    }
    
    class B { ... }
    
    public void MyMethod<T>(){
        T t = new T(); //This would be fine if you use 'MyMethod<B>' but you would have a problem calling 'MyMethod<A>' (because A doesn´t have a parameterless construtor;
    }
    

    To resolve this, you can tell the compiler that your generic parameter has a parameterless construtor. This is done by defining constraints:

    public void MyMethod<T>()  where T: new(){
        T t = new T(); //Now it's ok because compiler will ensure that you only call generic method using a type with parameterless construtor;
    }
    

    More information about constructor constraints may be found here: https://msdn.microsoft.com/en-us/library/bb384067.aspx

    0 讨论(0)
  • 2020-12-24 05:42

    You can't initialize Generic type object unless you mark it as implementing default constructor using new keyword:

    public void CountTestHelper<Item>() where Item : IHasRect, new()
     {
        Rectangle rectangle = new Rectangle(0, 0, 100, 100); 
        SomeClass<Item> target = new SomeClass<Item>(rectangle);            
        Point p = new Point(10,10);
        Item i = new Item();    // constructor has to be parameterless!
        ...
     }
    

    On the other hand, if you're trying to initialize an Item type object defined somewhere else in the application try using the namespace before it:

    MyAppNamespace.Item i = new MyAppNamespace.Item(p, 10);
    
    0 讨论(0)
提交回复
热议问题