Linked Questions
37 questions linked to/from Passing arguments to C# generic new() of templated type
96 votes
1 answer
45k views
Generics in C# - how can I create an instance of a variable type with an argument? [duplicate]
I've got a generics class, where I want to instantiate an object with the generic type. I want to use an argument for the constructor of the type. My code: public class GenericClass<T> where T ...
4 votes
1 answer
16k views
Generic method: instantiate a generic type with an argument [duplicate]
I have a generic method that takes in a type T, which i need to be able to call a constructor on that requires a single XmlNode. Currently, I am trying to do that by having an abstract base class that ...
0 votes
2 answers
3k views
How to use New() operator with parameters in a Generic class [duplicate]
Iam trying to make a generic class that receives a Type. this generic class will need to create an instance from the received type. The received type has two overloads in his constructor, one ...
0 votes
2 answers
327 views
Create Generic object inside class [duplicate]
I am making a base class from which other classes can be derived. public class BaseClass<T> where T { public BaseClass() { TClassObject = new T("SomeText"); // Error here } ...
-1 votes
1 answer
807 views
Setting readonly variables when initializing a generic-type object? [duplicate]
I have the following: a base class SystemBody (which has 3 readonly fields that must be set in the contructor, and never get changed in the object's lifetime), arbitrary derived classes from ...
1 vote
0 answers
733 views
Object Pooling with Generics [duplicate]
I am trying to create a manager for object pools such that I can call a method from my manager in order to retrieve a reserved object in the pool or to create a new object when necessary. The objects ...
0 votes
3 answers
219 views
Is it possible to have a generic method with constraint of constructor with parameter? [duplicate]
I have all kind of classes that represents entities, for example: public class Person { public int PersonID { get; set; } public string PersonName { get; set; } public bool ...
-2 votes
1 answer
201 views
C# generic method: what wrong and if possible to do this? [duplicate]
I'm trying to do something like this: public static T NewBinding2<T>(string pCfgName, string pCfgSuffix, string pAddr) where T : System.ServiceModel.Channels.Binding, new() { T ...
0 votes
2 answers
72 views
Can we pass parameters in constructor to a generic expression? [duplicate]
I want to take the Exception messages from the db and throw my custom exceptions with a generic structure. My custom exceptions have a construct that takes 2 parameters, string message and parameter ...
339 votes
13 answers
377k views
Create instance of generic type whose constructor requires a parameter?
If BaseFruit has a constructor that accepts an int weight, can I instantiate a piece of fruit in a generic method like this? public void AddFruit<T>()where T: BaseFruit{ BaseFruit fruit = ...
36 votes
5 answers
18k views
Does System.Activator.CreateInstance(T) have performance issues big enough to discourage us from using it casually?
Does System.Activator.CreateInstance(T) method have performance issues (since I'm suspecting it uses reflection) big enough to discourage us from using it casually?
29 votes
8 answers
95k views
How can I map the results of a sql query onto objects?
Currently, I am using something like this: try { dr = SQL.Execute(sql); if(dr != null) { while(dr.Read()) { CustomObject c = new CustomObject(); c....
19 votes
7 answers
13k views
C# generics problem - newing up the generic type with parameters in the constructor
I am trying to create a generic class which new's up an instance of the generic type. As follows: public class HomepageCarousel<T> : List<T> where T: IHomepageCarouselItem, new() { ...
26 votes
3 answers
11k views
Restricting a generic type parameters to have a specific constructor
I'd like to know why the new constraint on a generic type parameter can only be applied without parameters, that is, one may constraint the type to have the parameterless constructor, but one cannot ...
6 votes
5 answers
4k views
How to use new operator with a template in C#
I'm trying to figure out how to use templates in C#. I wrote this: public static List<TValue> deepCopyList<TValue>(List<TValue> src) { List<TValue> arr = new List<...