generics

Generic BitConverter.GetBytes possible in .NET?

Is it possible to create a method like BitConverter.GetBytes() that accepts as input also a parameter of type Object, without using Marshaling as done here? Or the only solution, if a type Object is given as input, is to implement a case on all available .NET value types? ...

Combining bounded wildcards in Java

Is there anyway to use a bounded wildcard require a class implement more than one interface? In otherwords, something like... class Foo<S extends Comparable && Clonable> ...which would require that objects extend both interfaces? I realize I can make another ComparableAndClonable which extends the two but I don't have control over...

Unexpected poor performance of SortedDictionary compared with Dictionary

I don't understand why the performance of SortedDictionary is approximately 5x slower than Dictionary for setting and retrieving values. I expected inserts and deletes to be slower but not updates or retrieves. I have tested .Net 3.5 and .Net 4.0 release compiled code. An array of random keys was pre-computed to ensure random variations ...

Using comparison operators, such as '!=' and '==', with generics constrained as value in C#

I have the following code: class Foo<T> where T : struct { private T t; [...] public bool Equals(T t) { return this.t == t; } } When I try to compile, it gives me the following error: Operator '==' cannot be applied to operands of type 'T' and 'T' Why can't it be done? If the constraint was where T : class it would h...

BindingSource with Generic SubClass in Windows Forms

I'm attempting to do what I considered simple data binding between a BindingSource and a ComboBox. I run into issues when the class I am using as the DataSource of the BindingSource has a property that is an instance of a generic class. I have the following generic class: public class GenericClass<T> { public T Code { get; set; ...

Is it possible to specify a generic constraint for a type parameter to be convertible FROM another type?

Suppose I write a library with the following: public class Bar { /* ... */ } public class SomeWeirdClass<T> where T : ??? { public T BarMaker(Bar b) { // ... play with b T t = (T)b return (T) b; } } Later, I expect users to use my library by defining their own types which are convertible to Bar...

Extract Generic types from extended Generic

I'm trying to refactor a class and set of subclasses where the M type does extend anything, even though we know it has to be a subclass of a certain type. That type is parametrized and I would like its parametrized types to be available to subclasses that already have values for M. Is there any way to define this class without having to...

What does this Java generics paradigm do and what is it called?

I'm looking at some Java classes that have the following form: public abstract class A <E extends A<E>> implements Comparable <E> { public final int compareTo( E other ) { // etc } } public class B extends A <B> { // etc } public class C extends A <C> { // etc } My usage of "Comparable" here is just to ...

Why do you need "? extends" in generics

Does anyone have a good explanation why the ? extends syntax has to be used in generics? I know what the syntax enforces but it just doesn't seem to be consistent with how non-generic type checking is done in Java. For example: void someMethod() { List<String> strings = new ArrayList<String>(); doSomethingWithStrings(strings);...

Confusing Java generics usage: how do I properly parameterize this?

I'm looking at some code with this form: 1 package com.stackoverflow.java.questions; 2 import java.util.ArrayList; 3 import java.util.List; 4 public class B extends A<B> { 5 6 private 7 <C extends A> 8 List<C> getList(Class<C> cls) { 9 10 Li...

Filter a list of objects using LINQ

I have a list of custom objects. These objects have 2 datetime properties on them. I need to get a list of unique (ignore the time part) datetime objects from the two objects properties. Example I have 2 objects with 2 datetime properties: object1.date1 = "01/01/2001 12:54" object2.date1 = "01/02/2001 12:51" object3.date1 = "01/01/20...

Problem with MessageContract, Generic return types and clientside naming

I'm building a web service which uses MessageContracts, because I want to add custom fields to my SOAP header. In a previous topic, I learned that a composite response has to be wrapped. For this purpose, I devised a generic ResponseWrapper class. [MessageContract(WrapperNamespace = "http://mynamespace.com", Wrappe...

java: interfaces and templates

This doesn't seem to work (compiler complains that Something's getFoo() method doesn't implement HasFoo) and I can't figure out why or how to fix it.... enum FooKey { BLOB1, DONUT, ... } interface HasFoo { public Object getFoo(FooKey k); } class Something implements HasFoo { private Map<FooKey, Object> map; @SuppressWarni...

Template/generic syntax character

I'm designing a syntax for templates/generics. The C++ family languages use angle brackets for this, but I'm looking at using a separator character instead. For example, where in Java you might write: Map<String, Foo> foos = new HashMap<String, Foo>(); I'm aiming for: .foos = hmap*string*foo I'm trying to decide what the separator ...

Class type issues

I have a class named "toto" which I send to a function that does the following: getData(toto); public String getData(Class myClass) { javax.jdo.Query query = p.newQuery(myClass); List<myClass> list = (List<myClass>) pm.newQuery(query).execute(); for (myClass element : list) { Do something here } } The problem is that I get ty...

Cannot Find Key in Generic Dictionary

I cannot find a dictionary entry by key. I have an interface like the following: public interface IFieldLookup { string FileName { get; set; } string FieldName { get; set; } } Then I have a dictionary like so: Dictionary<IFieldLookup, IField> fd When I try to retrieve an element out of the dictionary by the key, I get a Key...

Generic method for converting string array into list ...

I would like to create a function that will return list of type that is specified by me at run time. I tried something along this line: public static List<T> GetMyList<T>(string[] itemList) { List<T> resultList = new List<T>(itemList.Length); return resultList.AddRange(itemList); } But this doesn't work. Obviously I d...

How to write generic IEnumerable<SelectListItem> extension method

I'm fairly new (ok, REALLy new) to generics but I love the idea of them. I am going to be having a few drop-down lists on a view and I'd like a generic way to take a list of objects and convert it to a list of SelectListItems What I have now: public static IEnumerable<SelectListItem> ToSelectListItems( this IEnumerable<SpecificOb...

Can I have a class with a generic list and expose that as the default value

I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not shown Foreach (Person myPerson in myPersonList) { ... } Class declare public class PersonList { public List<Person> myIntenalList; Person CustomFunction() {...} } So how do I expose "myInternalList" in my class as the default value ...

Why should I replace CollectionBase with Generics?

I'm not looking for how, I'm looking for why? I couldn't find a straight forward answer to this. ...