generics

What is the difference between ? and Object in Java generics?

I'm using Eclipse to help me clean up some code to use Java generics properly. Most of the time it's doing an excellent job of inferring types, but there are some cases where the inferred type has to be as generic as possible: Object. But Eclipse seems to be giving me an option to choose between a type of Object and a type of '?'. So ...

How to Return Generic Dictionary in a WebService

Hello, I want a Web Service in C# that returns a Dictionary, according to a search: Dictionary<int, string> GetValues(string search) {} The Web Service compiles fine, however, when i try to reference it, i get the following error: "is not supported because it implements IDictionary." ¿What can I do in order to get this working?, any ...

Which is the right way to create an instance of Generic class in Java?

When initialising an instance of a Generic class in Java is there any benefit to specifying the Type on both sides of the statement? Or to put it another way, what's the difference between these two valid statements: ArrayList<String> test = new ArrayList<String>(); and: ArrayList<String> test = new ArrayList(); (It seems second s...

Type aliases for Java generics

I have a fairly complicated set of generic classes in Java. For example, I have an interface interface Doable<X,Y> { X doIt(Y y); } and the implementation class DoableImpl implements Doable<Foo<Bar<Baz,Qux>>,Foo<Bar<Zot,Qux>>> { Foo<Bar<Baz,Qux>> doIt(Foo<Bar<Zot,Qux>> fooBZQ) { ... } } In the real implementation, Doable has qu...

C#, XML-Doc: Refering to a generic type of a generic type in C# XML documentation?

Writing some xml documentation for a predicate helper class. But I can't figure out I can refer to an Expression<Func<T, bool>> without getting a syntax error. Is it even possible? I have tried this: <see cref="Expression{Func{T, bool}}"/> But I get a red squiggly line under {T, bool}}. This works though: <see cref="Expression{TDeleg...

Generic classes in Java (type-safely) depending on each other

Hello! I want to create an abstract collection class (called Space) and an abstract element class (called Atom). Instances of both have to know each other (exactly typed). That's the problem. abstract class Space<A extends Atom>{ // ... } abstract class Atom<S extends Space>{ // ... } Not good: "A extends Atom" means any Atom, ...

Static Generic Class as Dictionary

A static field in a generic class will have a separate value for each combination of generic parameters. It can therefore be used as a Dictionary<Type, whatever> Is this better or worse than a static Dictionary<Type, whatever>? In other words, which of these implementations more efficient? public static class MethodGen<TParam> { ...

Question about indexers and/or generics

Hello, how is it possible to know whether an object implements an indexer?, I need to share a logic for a DataRow and a IDataReader, but they don't share any interface. I tried also with generics but don't know what restriction should I put on the where clause. public class Indexer { // myObject should be a DataRow or a IDataReader...

Java Map equivalent in C#

I'm trying to hold a list of items in a collection with a key of my choice. In Java, I would simply use Map as follows: class Test { Map<Integer,String> entities; public String getEntity(Integer code) { return this.entities.get(code); } } Is there an equivalent way of doing this in C#? System.Collections.Generic.Hashset doe...

Powershell . Declare generic list with class defined using 'Add-Type'

I am trying to declare List in PowerShell, where the Person is defined using Add-Type: add-type -Language CSharpVersion3 -TypeDefinition @" public class Person { public Person() {} public string First { get; set; } public string Last { get; set; } } "@ This works fine: New-Object Person New-Object...

Test assignment for a C# generic type

Is there any way in which you can test whether an instance of a generic parameter in a generic method has been assigned to when it can be either a value or reference type? I would like to be able to do this in a generic method I have to persist types, where T is the instance and K is the type of the identifier field for that type (which ...

Should “Action” delegate be refactored out into a new method?

I have a following method private void SetProcessDocumentStatus(string status) { var setStatusWith = new Action<string>( statusValue => processDocumentStatusLabel.Text = statusValue); if (processDocumentStatusLabel.InvokeRequired) processDocumentStatusLabel.Invoke( (MethodI...

Given the names of the types as strings, how would I construct a generic using reflection?

Presuming I have the strings "List" and "Socket," how would I go about creating a List<Socket>? The answer I need will work just as well for Queue and XmlNodeList, not to mention MyCustomGeneric with MyCustomClass. ...

Wrapper Factory in Java

I may have designed myself into a corner with this problem but I feel like there's a workable known solution to something like this that I'm not seeing. It may well be that I'm completely overcomplicating the problem and skipped over the obvious solution. Any advice would be greatly appreciated. I have a set of entities defined as inter...

Narrow a Java field type without making the enclosing class generic?

Is it possible to narrow the type of a field in a Java class without making the containing class itself generic? The generic example would be something like this: abstract class MyClass { //... } interface MyInterface { //... } class MyConcreteClass<T extends MyClass & MyInterface> { private T value; } Is there any way ...

requiring the presence of static methods in C#

Given a group of objects that have a common set of properties and methods in support of given domain logic, is there a way of enforcing the presence of certain static methods on these objects? I have concluded that implementing an interface does not achieve this (methods are instance only) and that static methods can not be marked overr...

Examples of usage of Generics in .Net (C#/VB.Net)

What are some examples of you would use generics in C#/VB.Net and why would you want to use generics? ...

Operator as and generic classes

I'm writing .NET On-the-Fly compiler for CLR scripting and want execution method make generic acceptable: object Execute() { return type.InvokeMember(..); } T Execute<T>() { return Execute() as T; /* doesn't work: The type parameter 'T' cannot be used with the 'as' operator because it does not have a class type constraint nor a...

<?> vs <T>

I stumbled upon a function looking like this: public void function(Class<?> clazz) {...} What are the pros/cons of changing the method to: public <T> void function(Class<T> clazz) {...} edit: what are the compile time / runtime diff. ...

Java field type for a value of a generically recursive self-type?

Given a class hierarchy where the base class defines a recursive self-type: abstract class A<T extends A<T>> { } How can I declare another class (which should not be generic in T, because such a T could vary over the lifetime of the object) with a field that can hold any subclass of A? The following does not work: public class B { ...