Linked Questions

96 votes
1 answer
45k views

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 ...
Roman's user avatar
  • 4,521
4 votes
1 answer
16k views

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 ...
RCIX's user avatar
  • 39.7k
0 votes
2 answers
3k views

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 ...
XDev's user avatar
  • 165
0 votes
2 answers
327 views

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 } ...
fhnaseer's user avatar
  • 7,317
-1 votes
1 answer
807 views

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 ...
StarManta's user avatar
1 vote
0 answers
733 views

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 ...
Ryan T.'s user avatar
  • 219
0 votes
3 answers
219 views

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 ...
Liran Friedman's user avatar
-2 votes
1 answer
201 views

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 ...
dmitry_bond's user avatar
0 votes
2 answers
72 views

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 ...
Stick's user avatar
  • 154
339 votes
13 answers
377k views

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 = ...
Boris Callens's user avatar
36 votes
5 answers
18k views

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?
Pacerier's user avatar
  • 90.4k
29 votes
8 answers
95k views

Currently, I am using something like this: try { dr = SQL.Execute(sql); if(dr != null) { while(dr.Read()) { CustomObject c = new CustomObject(); c....
Legend's user avatar
  • 118k
19 votes
7 answers
13k views

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() { ...
ChrisCa's user avatar
  • 11.1k
26 votes
3 answers
11k views

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 ...
Amanda Tarafa Mas's user avatar
6 votes
5 answers
4k views

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<...
c00000fd's user avatar
  • 22.8k

15 30 50 per page