Hi people,
I would like to hear from you guys on how do you decide when you should be using concrete parameterized type vs. bounded parameterized type when designing API, esp. (that I care most) of defining a class/interface.
For instance,
public interface Event<S>{
void setSource(S s);
}
public interface UserEvent extends EVent<Us...
At the risk of becoming the village idiot, can someone explain to me why generics are called generics? I understand their usage and benefits, but if the definition of generic is "general" and generic collections are type safe, then why isn't this a misnomer?
For example, an ArrayList can hold anything that's an object:
ArrayList myObj...
At the XmlSerializer constructor line the below causes an InvalidOperationException which also complains about not having a default accesor implemented for the generic type.
Queue<MyData> myDataQueue = new Queue<MyData>();
// Populate the queue here
XmlSerializer mySerializer =
new XmlSerializer(myDataQueue.GetType());
StreamW...
I need a map that has two keys, e.g.
Map2<String /*ssn*/, String /*empId*/, Employee> _employees;
So that I can
_employees.put(e.ssn(), e.empId(), e)
And later
_employees.get1(someSsn);
_employees.get2(someImpId);
Or even
_employees.remove1(someImpId);
I am not sure why I want to stop at two, why not more, probably because th...
migrating an app from 1.1 to 2.0. should i remove all uses of collectionbase.. if so what is the best strategy for migration.
...
If have a set of classes that all implement an interface.
interface IMyinterface<T>
{
int foo(T Bar);
}
I want to shove them all in a list and enumerate through them.
List<IMyinterface> list
foreach(IMyinterface in list)
// etc...
but the compiler wants to know what T is. Can I do this? How can I overcome this issue?
...
With generics, is there ever a reason to create specific derived EventArg classes
It seems like now you can simply use them on the fly with a generic implementation.
Should i go thorugh all of my examples and remove my eventArg classes (StringEventArgs, MyFooEventArgs, etc . .)
public class EventArgs<T> : EventArgs
{
public EventA...
I like generics a lot and use them whereever I can. Every now and then I need to use one of my classes in another project which has to run on an old JVM (before 5.0), needs to run on JavaME (where generics are not allowed neither) or in Microsoft J# (which has VERY poor Support for generics).
At the moment, I remove all generics manuall...
I´m sure there´s a clever one-liner using the C++ stl generic algorithms for implementing the dot product of the elements in any ordered container, such as a vector or list. I just don´t seem to remember it!
The fancy implementation would be:
template <class containerT>
typename containerT::value_type dot_product (const containerT& lef...
I have a container filled with pairs. I want to iterate in it using the STL generic algorithms (in my case it would be inner_product, but consider it as a generic problem).
The algorithm I am using expects iterators first and last. Can I provide special iterators first and last that will iterate not on the pairs but on the first element ...
Am I doing something wrong or is it not possible to specify a generic class as a constraint to a generic method?
I have been playing around with generics and db4o (open source object database) and am writing a test program (see code below) to store and retrieve some user defined generic collections.
I am attempting to write a generic m...
What is the concept of erasure in generics in java?
...
Why doesn't the designer work if you inherit from an own written genericform?
Suppose I've got the following genericform
public class GenericForm<T> : System.Windows.Forms.Form
{
public T Test
{
get;
set;
}
}
When I go to the designer I get errors.
The only workaround I made up is using compiler directive...
The following code
public class GenericsTest2 {
public static void main(String[] args) throws Exception {
Integer i = readObject(args[0]);
System.out.println(i);
}
public static <T> T readObject(String file) throws Exception {
return readObject(new ObjectInputStream(new FileInputStream(file)));
...
Is there anything to use, to determine if a type is actually a anonymous type? For example an interface, etc?
The goal is to create something like the following...
//defined like...
public static T Get<T>(this IAnonymous obj, string prop) {
return (T)obj.GetType().GetProperty(prop).GetValue(obj, null);
}
//...
//And then used like...
What is the correct syntax for this:
IList<string> names = "Tom,Scott,Bob".Split(',').ToList<string>().Reverse();
What am I messing up?
What does TSource mean?
...
Aloha
I have a method with (pseudo) signature:
public static T Parse<T>(string datadictionary) where T : List<U>
This doesn't build. How can I restrict the in the method to accept only generic List<> objects (which should of cource not contain T's but something else :)
I need to restrict the type of T because I need to call a metho...
I understand that, if S is a child class of T, then a List<S> is not a child of List<T>. Fine. But interfaces have a different paradigm: if Foo implements IFoo, then why is a List<Foo> not (an example of) a List<IFoo>?
As there can be no actual class IFoo, does this mean that I would always have to cast each element of the list when exp...
I have a generic list of objects in C#, for example sake, here's what the object might be.
public class Thing {
public string Name { get; set; }
public DateTime EditDate { get; set; }
}
var things = new List<Thing>();
Now I want to call:
thing.Sort((t1, t2) => t1.EditDate.CompareTo(t2.EditDate));
However, some of my EditDa...
Is it possible to cast an object in Java to a combined generic type?
I have a method like:
public static <T extends Foo & Bar> void doSomething(T object) {
//do stuff
}
Calling this method is no problem if I have a class that implements both interfaces (Foo & Bar).
The problem is when I need to call this method the object I need...