generics

C# Generic overloading of List<T> : How would this be done?

The StringBuilder class allows you, in what I consider to be a very intuitive way, to chain method calls to .Append(), .AppendFormat() and some others like so: StringBuilder sb = new StringBuilder(); sb.Append("first string") .Append("second string); The List class' .Add() method, on the other hand, returns void - so chaining calls ...

C# generic does *not* implement something

I know I can make a method like private T MyFun<T>() where T : IMyInterface {...} Can I do the reverse, i.e. where T does not implement IMyInterface? The specific use case is that I don't want to allow nullables, but I'm curious just in general. ...

Invoking a method of a Generic Class

Here is the Context : I try to code a mapper for converting my DomainModel Objects to ViewModel Ojects dynamically. The problem I get, it's when I try to invoke a method of generic class by reflection I get this error : System.InvalidOperationException : Late bound operations cannot be performed on types or methods for which ContainsGe...

Yet another java generics confusion

Let us have the following code: public class TestGenerics { static <T> void mix(ArrayList<T> list, T t) { System.out.println(t.getClass().getName()); T item = list.get(0); t = item; System.out.println(t.getClass().getName()); } public static void main(String[] args) { ArrayList<Object>...

Returning a generic List from a function

As an exercise I'm trying to create a function that returns a generic list from another generic list that's passed to the function. The list passed in could be of either int or string, and I want to return a list of all members of the passed in list that have an even number of characters. The way I'm doing it isn't correct and it doesn't...

java generics super keyword

I went through this topics http://stackoverflow.com/questions/591004/generics-super-t/591011#591011 http://stackoverflow.com/questions/2800369/bounding-generics-with-super-keyword However, I still seem to be kind of lost with super keyword: When we declare a collection like that: List<? super Number> list = null; list.add(new Inte...

Static members and templates in c++

Given the code: #include <iostream> using namespace std; template <typename T> T my_max (const T &t1, const T &t2) { static int counter = 0; counter++; cout << counter << " "; return ((t1 > t2) ? t1 : t2); } int main() { my_max (2,3); my_max (3.5, 4.3); my_max (3,2); my_max ('a','c'); } The output is: ...

Determining the Type for a generic method parameter at runtime

Given the a class with the following structure. I am trying to determine the type of the parameter T assigned by the caller of the generic method. public class MyClass{ public <T> Boolean IsSupportable() { Class typeOfT; // Need to determine Class for generic parameter T // Business Logic goes here retu...

Java: Generic methods and numbers.

Hi, I want to make a generic method which makes the total sum of a List of numbers. What I was trying is this: public static <T extends Number> T sumList(List<T> data) { T total = 0; for (T elem : data) { total += elem; } return total; } But the problem is that there is no += operator in T and that total ...

A more generic return

Looking at some code cleanup and I was wondering the best way to deal with this: Have a class with some private variables like: myBool1, myBool2, myBool3 myInt1, myInt2, myInt3 myString1, myString2, myString3 What's the best way to do a getter function that is generic to the return value? So if I do a call to the getter with somethi...

C# - what is wrong with the code (generics, static method etc)

I'm getting a "cannot convert from 'int' to 'TValue'" in the following code. How can I fix this? I understand why there is an issue, but I'm wonder the best way around this. Do I need to make a decision to specific type the ReturnValuesDict, and not leave it generic? public class ReturnValuesDict<TKey, TValue> : CloneableDictionary<T...

Array type in generics

I am trying to create an array of generic type. I am getting error: Pair<String, String>[] pairs; // no error here pairs = new Pair<String, String>[10]; // compile error here void method (Pair<String, String>[] pairs) // no error here. I am confused. Any clues why this is happening. ...

How to specify a type parameter which does NOT implement a particular interface?

I have developed some extension methods for objects, which I don't want to be used/shown in intellisense for objects which implements IEnumerable. Conceptually I want something like as follows public static T SomeMethod<T>(this object value) where T != IEnumerable { } Is it possible to impose this kind of constraint a...

Generic problem with List

I'm trying to make a Parallel Helper class (ParallelRunner) that can be used by like in the RequestService class: public class ParallelFunction { public ParallelFunction(Func<object> function) { Function = function; } public T GetResult<T>() where T : class { return (T) Data; } public Func<o...

Is this possible using generics? c#

I know I can solve the following problem just by creating a custom class, but can the following be strongly typed as a List(or any other type)? var x = new object[] { new object[] { new Image(), new TextBox(), new FileUpload() }, new object[] { new Image(), new TextBox() , new FileUpload()} ...

How do I use a generic base class two levels down in an inheritance tree?

Imagine that I have a generic base class like this: public abstract class AnimalDTO<TA, TB> { public static TB ToDTO(TA entity) { return ConvertToDTO<TB>(entity); } } The class is responsible for being able to convert a passed-in entity to a DTO. I have a class that uses this generic class: public class MammalDTO...

How would contravariance be used in Java generics?

In Java, covariance allows the API designer to specify that an instance may be generalised as a certain type or any of that type's subtypes. For example: List<? extends Shape> shapes = new ArrayList<Circle>(); // where type Circle extends Shape Contravariance goes the other way. It allows us to specify that an instance may be general...

Static Method Overloading with Generics

Where I try to create two static overloading methods I got an compilation error. Can anyone explain this public class A { public static void a(Set<String> stringSet) {} public static void a(Set<Map<String,String>> mapSet) {} } ...

Generic constraints: Can I test Equality of generic that can be a reference or value type?

I want a single generic class that can accept either reference or value types, and only perform an action based on an equality test. consider the following: public class Property<TProp> where TProp : struct, IEquatable<TProp> { public TProp Value; public void SetValue(ObservableObject owner, TProp value) { if (!Value.E...

C# Class Inheritance

I am working with insurance and have two different policy types - motor and household, represented by two different classes, Motor and Household. Both have several bits of data in common, so both would inherit from another class called Policy. When a user logs into the app, they could have either a motor or a household policy, so the a...