generics

is combined generic bounds an anti pattern?

as a follow up on my previous question Having a function with combined generic bounds such as: <T extends Foo & Bar> void doStuff(T argument) { //do stuff wich should only be done if arguments is both foo and bar } Because this is not castable from a unspecified object, you need to have knowledge of some object which actually implem...

Generic ThreadPool in .NET

Here's a relatively common task for me, and, I think, for many a .NET programmer: I want to use the .NET ThreadPool for scheduling worker threads that need to process a given type of tasks. As a refresher, the signatures for the queueing method of the ThreadPool and its associated delegate are: public static bool QueueUserWorkItem ( ...

Calling generic method with a type argument known only at execution time

Edit: Of course my real code doesn't look exactly like this. I tried to write semi-pseudo code to make it more clear of whay I wanted to do. Looks like it just messed things up instead. So, what I actually would like to do is this: Method<Interface1>(); Method<Interface2>(); Method<Interface3>(); ... Well ... I thought that maybe I...

How to return an array back from a function from a LINQ annonymous list

Hi all, Essentially i want to have a generic function which accepts a LINQ annonymous list and returns an array back. I was hoping to use generics but i just can seem to get it to work. hopefully the example below helps say i have a person object with id, fname, lname and dob. i have a generic class with contains a list of objects. i...

Java Generic voodoo has me stumped

I'm wanting to write a method that I can use to initialise a Map. First cut: Map map(Object ... o) {for (int i = 0; i < o.length; i+=2){result.put(o[i], o[i+1])}} Simple, but not type-safe. Using generics, maybe something like: <TKey, TValue> HashMap<TKey, TValue> map(TKey ... keys, TValue ... values) but that syntax isn't support...

How do I declare a TDictionary enumerator?

I've got a TDictionary that stores a bunch of objects indexed by name, and I'd like to be able to examine all of the objects. So I tried this: var enumerator: TMyObject; begin for enumerator in myDictionary do But that wouldn't compile. "Incompatible types: 'TMyObject' and 'TPair' So I tried it a bit differently: var enumerator:...

Moving to generics . .

I am migrating a 1.1 winforms app to 2.0. what are the main things i should immediately change because of generics. Here what i have so far: Replace all hashtables with generic dictionaries Replace all arraylists with List<> Replace all CollectionBase derive classes with : List<> Any others that should be done immediately? thks, a...

Hashtable to Dictionary<> syncroot . .

Hashtables have a syncroot property but generic dictionaries dont. If i have code that does this: lock (hashtable.Syncroot) { .... } How i do i replicate this if i am removing the hashtable and changing to generic dictionaries. ...

DictionaryBase to generics . .

if i have a .net 1.1 class that inherits from DictionaryBase and i am moving this to 2.0.. what is the generics equivalent to change this to? ...

ICollection<string> to string[]

i have a generic ICollection. what is the best way to convert to string[]. using dotnet 2.0 ...

Generics and System.Collections

After moving to .NET 2.0+ is there ever a reason to still use the systems.Collections namespace (besides maintaining legacy code)? Should the generics namespace always be used instead? ...

Class inherits generic dictionary<string, IFoo> and Interface

I have a class that inherits a generic dictionary and an inteface public class MyDictionary: Dictionary<string, IFoo>, IMyDictionary { } the issue is that consumers of this class are looking for the '.Keys' and ".Values" properties of the interface so i added: /// <summary> /// /// </summary> ICollection<string> Keys...

IDictionary<string, string> versus Dictionary<string, string>

what is the value of using IDictionary here? ...

IEnumerator to generics . .

i have a few classes that i am trying to move to using generics Class1: Curve this class has the following code: public class Curve : IEnumerable IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); // Calls non-interface method } public RTRatePointEnumerator GetEnumerator() { return new ...

Strongly Typed Controls in .NET

I am working on a Windows Forms app for quite some time now, and I really find myself doing more typecasts in the GUI code than I ever did in my underlying business code. What I mean becomes apparent if you watch the ComboBox control that accepts some vague "object" as it's item. Then you go off and may display some DisplayMember and a...

How to compare two elements of the same but unconstrained generic type for equality?

I've got the following generic class and the compiler complains that "Operator '!=' cannot be applied to operands of type 'TValue' and 'TValue'" (see CS0019): public class Example<TValue> { private TValue _value; public TValue Value { get { return _value; } set { if (_value != value) // <<...

.Net framework version in Silverlight: no List<T>.Find methods?

Today I discovered something that makes me sad: objects of type System.Generic.Collections.List don't have a number of the useful extension methods I've come to love, such as Find, FindAll, FindIndex, Exists, RemoveAll, etc. The object browser in VS2008 shows that those methods exist in the mscorlib version I'm using, but if I look at ...

Java Generics: Generic type defined as return type only

I'm looking at some GXT code for GWT and I ran across this use of Generics that I can't find another example of in the Java tutorials. The class name is com.extjs.gxt.ui.client.data.BaseModelData if you want to look at all of the code. Here are the important parts: private RpcMap map; public <X> X get(String property) { if (allowNes...

Java generics - type erasure - when and what happens

I read about Java's type erasure on Sun's website (http://java.sun.com/docs/books/tutorial/java/generics/erasure.html). I have the following question : When does type erasure occur - at compile time / runtime:when the class is loaded / runtime:when the class is instantiated. A lot of sites (including the Sun tutorial mentioned above) ...

C# to VB.Net: Why does this fail to compile when converted to VB?

I have this C# extension method that will extend any dictionary where the Value type is an IList. When I write the equivalent code in VB.Net I get the following compile error: "Extension method 'Add' has some type constraints that can never be satisfied". I find this really puzzling as the same type constraints can be satisfied in...