Generic pair class

Almost. I’d write it like this: public class Pair<F, S> { private F first; //first member of pair private S second; //second member of pair public Pair(F first, S second) { this.first = first; this.second = second; } public void setFirst(F first) { this.first = first; } public void setSecond(S second) { this.second = second; … Read more

Java generics and array initialization

It’s because you can’t create, but you can use them: public class GenericsTest { //statement 1 public ArrayList<Integer>[] lists; public GenericsTest() { //statement 2 lists = new ArrayList[4]; //statement 3 lists[0].add(new Integer(0)); //statement 4 lists[0].add(new String(“”)); } } Statement 3 is possible, statement 4 will lead to a compiler error.

ASP.NET MVC Model Binder for Generic Type

Create a modelbinder, override BindModel, check the type and do what you need to do public class MyModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (HasGenericTypeBase(bindingContext.ModelType, typeof(MyType<>)) { // do your thing } return base.BindModel(controllerContext, bindingContext); } } Set your model binder to the default in the global.asax protected void … Read more

Creating circular generic references

Circular generic references are indeed possible. Java Generics and Collections includes several examples. For your case, such a specimen would look like this: public interface P2PNetwork<N extends P2PNetwork<N, C>, C extends P2PClient<N, C>> { void addClient(C client); } public interface P2PClient<N extends P2PNetwork<N, C>, C extends P2PClient<N, C>> { void setNetwork(N network); } class TorrentNetwork … Read more

Convert DataTable to Generic List in C#

You could actually shorten it down considerably. You can think of the Select() extension method as a type converter. The conversion could then be written as this: List<Cards> target = dt.AsEnumerable() .Select(row => new Cards { // assuming column 0’s type is Nullable<long> CardID = row.Field<long?>(0).GetValueOrDefault(), CardName = String.IsNullOrEmpty(row.Field<string>(1)) ? “not found” : row.Field<string>(1), }).ToList();

What is the use and point of unbound wildcards generics in Java?

An unbound type can be useful when your method doesn’t really care about the actual type. A primitive example would be this: public void printStuff(Iterable<?> stuff) { for (Object item : stuff) { System.out.println(item); } } Since PrintStream.println() can handle all reference types (by calling toString()), we don’t care what the actual content of that … Read more

How does Java’s use-site variance compare to C#’s declaration site variance?

I am just going to answer the differences between declaration-site and use-site variance, since, while C# and Java generics differ in many other ways, those differences are mostly orthogonal to variance. First off, if I remember correctly use-site variance is strictly more powerful than declaration-site variance (although at the cost of concision), or at least … Read more