Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
 Compiler Error CS0417

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual C# Reference: Errors and Warnings
Compiler Error CS0417

Error Message

'identifier': cannot provide arguments when creating an instance of a variable type

This error occurs if a call to the new operator on a type parameter has arguments. The only constructor that can be called using the new operator on an unknown parameter type is a constructor with no arguments. If you need to call another constructor, consider using a class type constraint or interface constraint.

The following example generates CS0417:

// CS0417
class C<T> where T : new()
{
    T type = new T(1);   // CS0417
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Constructor constraint only works with default constructor      Griestopf   |   Edit   |   Show History

You cannot call other constructors than the default constructor. Something like

class TBase
{
private int ii;
public TBase(int i)
{
ii = i;
}
}
 
class C<T> where T : TBase
{
T type = new T(42);
}

doesn't work!

You can work around this issue using Activator.CreateInstance      dtackett   |   Edit   |   Show History
public class DataContextFactory<T> where T : DataContext
{
public static T GetDataContext()
{
T retval = (T) Activator.CreateInstance(typeof(T),
new object[] { GetConnectionString() });
return retval;
}
..
..
}
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker