Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I enforce derived classes to have parameterless constructors?

Tags:

c#

generics

I am trying to do the following:

class Program
{
    static void Main(string[] args)
    {
        foo<baz> fooObject = new foo<baz>();
        AnotherClass<baz> _baz = new AnotherClass<baz>();
        _baz.testMethod(fooObject);
    }
}

public class AnotherClass<T> where T : bar
{
    public void testMethod(foo<T> dummy)
    {
        foobar = dummy;
    }

    private foo<T> foobar = null;
}

public class foo<T> where T : bar, new()
{
    public foo()
    {
        _t = new T();
    }

    private T _t;

}

public abstract class bar
{
    public abstract void someMethod();
    // Some implementation
}

public class baz : bar
{
    public override void someMethod()
    {
        //Implementation
    }
}    

And I get an error explaining that 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method. I fully understand why this must be, and also understand that I could pass a pre-initialized object of type 'T' in as a constructor argument to avoid having to 'new' it, but is there any way around this? any way to enforce classes that derive from 'bar' to supply parameterless constructors?

Fixed - I was missing a 'new()' constraint on AnotherClass().

like image 943
Ben Heymink Avatar asked Apr 16 '10 08:04

Ben Heymink


People also ask

What is a Parameterless constructor?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new .

How to initialize constructor in c#?

Default Constructor Program p1 = new Program(); Here, C# automatically creates a default constructor. The default constructor initializes any uninitialized variable with the default value.

How to define constructor in c# net?

Note that the constructor name must match the class name, and it cannot have a return type (like void or int ). Also note that the constructor is called when the object is created. All classes have constructors by default: if you do not create a class constructor yourself, C# creates one for you.

What is the purpose of a constructor c#?

The main use of constructors is to initialize the private fields of the class while creating an instance for the class. When you have not created a constructor in the class, the compiler will automatically create a default constructor of the class.


2 Answers

The correct syntax is

public class foo<T> where T : bar, new()
like image 148
Gerrie Schenck Avatar answered Sep 27 '22 16:09

Gerrie Schenck


Aren't you missing a new() constraint on your AnotherClass?

public class AnotherClass<T> where T : bar, new()

Without that VS2010 refuses to compile, with that it works just fine.

like image 40
Ian Mercer Avatar answered Sep 27 '22 16:09

Ian Mercer