Explain Generics in layman style in C#?
Duplicate of http://stackoverflow.com/questions/155260/what-is-the-functionality-within-c Actually i want to know 'why and when should i use generics?'. What is the need for it? ...
Duplicate of http://stackoverflow.com/questions/155260/what-is-the-functionality-within-c Actually i want to know 'why and when should i use generics?'. What is the need for it? ...
Hi all, In a project I'm currently working on, we've added a wrapper class for accessing the HttpSessionState object. The trouble is that the current solution means you have to write some code to wrap the functionality. I came up with the following solution /// <typeparam name="TKey">Class used for generating key into session state sto...
Based on some advice I found on StackOverflow, I'm digging into Haskell. I was pleased to see that Haskell's parameterized types behave very much like C# generics. Both languages advise a single letter for the type parameter (usually), and both languages seem to follow a similiar process for substituting an actual type for the type param...
Sorry to ask this yet again. I've been reading the related topics here for hours and still don't quite get some of the issues with casting and generics. I'm trying to create our own HashSet class, because we can't use .net 3.5 yet. The error is in the RetainAll method, below. It compiles fine but I get a runtime error 'Unable to cast...
Two simple questions about generics. Are the following two function definitions the same? FunctionA(Exception ex); FunctionB<T>(T ex) where T : Exception; Are there advantages the Generic implementation (FunctionB) has over the normal implementation (FunctionA)? ...
Hi, This is kind of two questions (one more specific than the other). If I have a method like this: Public Function Blah(String Foo) End Function Can I qualify Foo against another type (for instance can I require that Foo be a String that also implements IInterface?). I'm imagining something vaguely similar to this: Public Functio...
I'm considering porting a very simple text-templating library to scala, mostly as an exercise in learning the language. The library is currently implemented in both Python and Javascript, and its basic operation more or less boils down to this (in python): template = CompiledTemplate('Text {spam} blah {eggs[1]}') data = { 'spam': 1, 'e...
I'm trying to add a constraint to a generic method so it checks for ValueTypes, Strings or Nullable value types. The problem is that: value types are struts strings are immutable reference types nullable are value types but won't be accepted in a "where S : struct" type constraint. So does anybody know if there's a way I can accep...
All APIs in Hibernate are from 1.4 and are thus not using java generics. So I wonder how "safe" the following is: (preconditions: name column is of datatype String, or atleast compatible to String) @SuppressWarnings("unchecked") public List<String> getAll() { Query q = session.createQuery( "select name from Customers"); ...
What a great site this is, I have lurked on here reading others questions for ages but now I have one of my own. My workmate wrote a class very like the one below. As soon as I saw it I knew it wouldn't work but I have no explination for him why it doesn't work. What he expected when declaring it as a ControlItem<Button> is that the...
Hi, I got a liiitle problem. There is List<List<UInt32>> temp = new List<List<UInt32>>(); For example, there are two List<UInt32> records within the List temp however, when i try to do something like temp.removeAt(0); it doesn't remove the first row (List<UInt32>) .. Why is that? Do i do something wrong? Update Here is the code th...
I've used this ObjectPool class as the basis of my Identity Map. However, I need to bring back the list of all objects of a type. Matthew has : public IEnumerable<T> GetItems<T>() { Type myType = typeof(T); if (!m_pool.ContainsKey(myType)) return new T[0]; return m_pool[myType].Values as I...
I have a list of objects that I want to filter by an integer parameter List<testObject> objectList = new List<testObject>(); // populate objectList with testObjects objectList.FindAll(GroupLevel0); private static bool GroupLevel0(testObject item) { return item._groupLevel == 0; } private class testObject { public string _FieldS...
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 = new T(weight); /*new Apple(150);*/ fruit.Enlist(fruitManager); } An example is added behind comments. It seems I can only do this if I give ...
I wrote a linked list implementation for my java class earlier this year. This is a generic class called LList. We now have to write out the merge sort algorithm for a lab. Instead of creating a new List implementation that takes Ints, I decided to just reuse the generic list I had created before. The problem is how do I compare two gen...
I really should be able to get this, but I'm just to the point where I think it'd be easier to ask. In the C# function: public static T GetValue<T>(String value) where T:new() { //Magic happens here } What's a good implementation for the magic? The idea behind this is that I have xml to parse and the desired values are often primi...
Check the code. class DynamicObj : BaseObj {} class BaseObj {} class clientCode { List<DynamicObj> GetFilteredObjs(List<BaseObj> baseList) { // I want to return the sublist of baseList which only have DynamicObj. List<DynamicObj> dList = baseList.FindAll( delegate(BaseOb...
Hi, I need to write a tree search method which takes a type parameter T and returns all items of type T that exist in the tree. Is there any way to do this? I would prefer elegance over efficiency at this point... ...
I have class like this: Class foo<T> { anotherfoo<T>; foo(){} foo(foo<T> aFoo) { anotherfoo = aFoo; } } void main() { foo<string> obj1 = new foo<string>(); foo<int> obj2 = new foo<int>(obj1); } This time I get a error: cannot convert from foo<string> to foo<int>. But I need have in this class "foo" another obj foo of anoth...
I wrote the following method: public T GetByID(int id) { var dbcontext = DB; var table = dbcontext.GetTable<T>(); return table.ToList().SingleOrDefault(e => Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e, null)) == id); } Basically it's a method in a Generic class where T is a class in a DataContext. The m...