generics

Passing an empty IEnumerable argument to a method

I have this method (simplified): void DoSomething(IEnumerable<int> numbers); And I invoke it like this: DoSomething(condition==true?results:new List<int>()); The variable results is formed with a LINQ select condition (IEnumerable). I was wondering is this List<int>() the best way (the fastest?) to pass an empty collection, or is ...

Java generics method overriding

I have interface: public interface CartService extends RemoteService{ <T extends ActionResponse> T execute(Action<T> action); } I wish override 'execute' method in implementation: public class XX implements CartService { @Override public <GetCartResponse> GetCartResponse execute(GetCart action) { // TODO Auto-generated m...

How to infer TResult from the actual Type?

I am using the Entity Framework, ASP.NET and C#3.5 I borrowed the following code to make sorting possible using a sortExpression from a GridView instead of the property of an entity: public static IEnumerable<T> Sort<T>(this IEnumerable<T> source, string sortExpression) { string[] sortParts = sortExpression.Split(' '); ...

using STI and ActiveRecordBase<> with full FindAll

Is it possible to use generic support with single table inheritance, and still be able to FindAll of the base class? As a bonus question, will I be able to use ActiveRecordLinqBase<> as well? I do love those queries. More detail: Say I have the following classes defined: public interface ICompany { int ID { get; set; } string...

How can I add to List<? extends Number> data structures?

I have a List which is declared like this : List<? extends Number> foo3 = new ArrayList<Integer>(); I tried to add 3 to foo3. However I get an error message like this: The method add(capture#1-of ? extends Number) in the type List<capture#1-of ? extends Number> is not applicable for the arguments (ExtendsNumber) ...

Java: Is it possible to have a generic class that only takes types that can be compared?

I wanted to do something along the lines of: public class MyClass<T implements Comparable> { .... } But I can't, since, apparently, generics only accept restrictions with subclasses, and not interfaces. It's important that I'm able to compare the types inside the class, so how should I go about doing this? Ideally I'd be able to ...

Is there any significant difference between List.Last() and List.Last<T>()?

Given a list of rectangles, var myList = new List<Rectangle>(); I cannot add anything but Rectangles to this list, so what factors would make me prefer Rectangle lastRect = myList.Last<Rectangle>(); over simply Rectangle lastRect = myList.Last(); ...

When is a parameterized method call useful?

A Java method call may be parameterized like in the following code: class Test { <T> void test() { } public static void main(String[] args) { new Test().<Object>test(); // ^^^^^^^^ } } I found out this is possible from the Eclipse Java Formatter settings dialog and wondered if there are...

Is this a covariance bug in C# 4 ?

In the following piece of code I expected to be able to implicitly cast from elements to baseElements because TBase is implicitly convertible to IBase. public interface IBase { } public interface IDerived : IBase { } public class VarianceBug { public void Foo<TBase>() where TBase : IBase { IEnumerable<TBase> elements = n...

How can I make this code more generic

Hi How could I make this code more generic in the sense that the Dictionary key could be a different type, depending on what the user of the library wanted to implement? For example someone might what to use the extension methods/interfaces in a case where there "unique key" so to speak for Node is actually an "int" not a "string" for e...

Is this a good way to generically deserialize objects?

I have a stream onto which serialized objects representing messages are dumped periodically. The objects are one of a very limited number of types, and other than the actual sequence of bytes that arrives, I have no way of knowing what type of message it is. I would like to simply try to deserialize it as an object of a particular type,...

Typesafe fire-and-forget asynchronous delegate invocation in C#

I recently found myself needing a typesafe "fire-and-forget" mechanism for running code asynchronously. Ideally, what I would want to do is something like: var myAction = (Action)(() => Console.WriteLine("yada yada")); myAction.FireAndForget(); // async invocation Unfortunately, the obvious choice of calling BeginInvoke() without a ...

How to implement == or >= operators for generic type

I have a generic type Foo which has a internal generic class Boo. Boo class a property Value of type K. In a method inside Foo i want to do a boo.Value >= value Note that second operand value is of type T. while compiling i am getting following error: Operator '>=' cannot be applied to operands of type 'T' and 'T' Can anyone please...

Is is possible to do an end-run around generics covariance in C# < 4 in this hypothetical situation?

Suppose I have a small inheritance hierarchy of Animals: public interface IAnimal { string Speak(); } public class Animal : IAnimal { public Animal() {} public string Speak() { return "[Animal] Growl!"; } } public class Ape : IAnimal { public string Speak() { return "[Ape] Rawrrrrrrr!"; } } pub...

F# Class with Generics : 'constructor deprecated' error

I am trying to create a a class that will store a time series of data - organized by groups, but I had some compile errors so I stripped down to the basics (just a simple instantiation) and still can't overcome the compile error. I was hoping some one may have seen this issue before. Clas is defined as: type TimeSeriesQueue<'V, 'K whe...

Generic collection class?

Is there anyway I can do this? class TSOC<TCollection, TValue> : ICollection<TValue>, INotifyCollectionChanged where TCollection : ICollection { private TCollection<TValue> collection; } It doesn't like my definition of collection. I'd prefer the definition to look like TSOC<TCollection> where the user can pass in List<int> or som...

Java generics - getting the type..

Duplicate: Instantiating generics type in java Hi! I'm a c# guy giving Java a try .. so how would I do the following in java. in C# public T create_an_instance_of<T>(){ T instance = default (T); // here's usually some factory to create the implementation instance = some_factory.build<T>(); // or even.. instance = some_...

Passing the Class<T> in java of a generic list?

I have a method for reading JSON from a service, I'm using Gson to do my serialization and have written the following method using type parameters. public T getDeserializedJSON(Class<T> aClass,String url) { Reader r = getJSONDataAsReader(url); Gson gson = new Gson(); return gson.fromJson(r, aClass); } I'm consuming json wh...

Bounding generics with 'super' keyword

Why can I use super only with wildcards and not with type parameters? For example, in the Collection interface, why is the toArray method not written like this interface Collection<T>{ <S super T> S[] toArray(S[] a); } ...

Java class object from type variable

Is there a way to get Class object from the type variable in Java generic class? Something like that: public class Bar extends Foo<T> { public Class getParameterClass() { return T.class; // doesn't compile } } This type information is available at compile time and therefore should not be affected by type erasure, so, t...