generics

Generic Constraints vs. Inheritance

I'm trying to write some code to help unit test WCF services. These services are accessed through a facade class that creates the proxy instance, then calls the proxy method and returns the result; for each proxy method. I'd like to be able to replace the current creation code with something that either creates the real service or a fake...

What is the Java equivalent of C++'s templates?

What is the Java equivalent of C++'s templates? I know that there is an interface called Template. Is that related? ...

Why can't add Strings in List?

As we know know that we can add strings and integers in list if we declare generic type for list. but the code seems not adding up the strings. what is the problem i can't understand.. class GHJ{String s;GHJ(String s){this.s=s;}} class DFR{ public static void main(String[] g){ List<? extends Object> list=new ArrayList(); ...

How can I create a generic method to return a specific type specified by the call?

The following code gives me the errors: Cannot implicitly convert type T to string. Cannot implicitly convert type T to int. What do I have to do to get this method to return the type of variable I define with T when I call it? using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestGener2...

Converting IEnumberable<T> to List<T> on a LINQ result, huge performance loss.

On a LINQ-result you like this: var result = from x in Items select x; List<T> list = result.ToList<T>(); However, the ToList<T> is Really Slow, does it make the list mutable and therefore the conversion is slow? In most cases I can manage to just have my IEnumerable or as Paralell.DistinctQuery but now I want to bind the items to a...

c# generic method overload not consistent with abstract Visitor pattern

Hello *, experimenting with Visitor pattern and generic method I found a kind of discrepancy in C#.NET. AFAIK C# compiler prefers an explicit overload to a generic method, therefore the following code: public abstract class A { public abstract void Accept(Visitor v); } public class B : A { public override void Accept(Visitor v...

Java generic type

When I have an interface public interface Foo<T> { T someMethod(); } is there any way to assure that when some class implements this interface then generic type is the same implementig class. For example: public class Bar implements Foo<Bar> { Bar someMethod() { return new Bar(); } } ...

Generic interfaces

Here is my code public interface ITranslator<E, R> { E ToEntity<T>(R record); } class Gens : ITranslator<string, int> { #region ITranslator<string,int> Members public string ToEntity<MyOtherClass>(int record) { return record.ToString(); } #endregion } When I compile this, I get an error Type p...

Java - Can you access an Enum declared in Subclass from Super class?

I was hoping to declare in Enum type in a subclass and then access it from the super class. This is what I came up with, but it does not work: class Subclass { enum Pets { CAT, DOG; } Class<Pets> getEnumClass() { return Pets.class; } } class Superclass { // This generates a warning: abstract Class<? extends Enum> ge...

Implementing a generic interface with a raw type

I have a generic tree, the generic parameter is the data type stored by the nodes: class TreeNode<D>{ public D data; ..... } Then a visitor interface to use along with a tree transversal: interface Visitor<D> { void visit(TreeNode<D> node); } Some visitors can take advantage of generics: class DataListCreator<D>...

.Net: What exactly is an "open generic type"

I was going through Asp.Net MVC lesson and learned that, for a method to qualify as an action for a controller, It must not have an "open genric type" I understand generics some how and use them as well to some extent, but what is an open generic type in .Net. Is there something as closed generic type ? open genric type is a term ...

java unchecked cast

I have a comparator class in Java to compare Map entries: public class ScoreComp implements Comparator<Object> { public int compare(Object o1, Object o2) { Entry<Integer, Double> m1 = null; Entry<Integer, Double> m2 = null; try { m1 = (Map.Entry<Integer, Double>)o1; m2 = (Map.Entry<...

How to add Type Parameters to XAML

I have the following class: public class DefaultDropTargetAdvisor : IDropTargetAdvisor { I decided to make it have a generic parameter: public class DefaultDropTargetAdvisor<TDragType> : IDropTargetAdvisor where TDragType : class { This was my XAML before the change: <local:DefaultDropTargetAdvisor x:Key="targetAdvisor1"/>...

generic FIFO class to use for storing bytes and doing InputStream.read() [Java 5 and up]

I'm thinking about using as a base some FIFO (for example, BoundedFifoBuffer from Apache Commons) to organize IO tasks in my application (I was looking for something easy to use as a temporary buffer for reading from a stream and FIFO simultaneously: so I will read data from an InputStream to it and then consume the data later; in my cas...

Serializing Generic Classes

I have read around that serializing generic classes is not supported out of the box with XamlWriter. First I would like to know why? What is harder about generic classes that makes them non-plug-and-play like all the other classes are. Second, is there a framework that will allow me to serialize my generic class without much work. (M...

PropertyGrid GenericList with GenericItems

Is there a way to display and edit values in the PropertyGrid (and his CollectionEditor) of an object, which is derived from an abstract generic class? I don't get the properties displayed only something like this: "IFilter´1" or "BaseFilter'1" Where IFilter is an Interface, and BaseFilter an abstract class. All objects contained by thi...

Are there any collections in .NET that prevent null entries?

I'm specifically thinking about a collection that fulfills the contract of a set, but I think the question can apply to any kind. Are there collections in the .NET framework that prevent null entries? The specific behavior I want is this: var set = new HashSet<object>(); bool added = set.Add(null); Console.WriteLine(added); // prints "F...

Default value check using generic types

I want to be able to check whether a value is the default for its value type. Ideally, I'd like to say: DoSomething<TValue>(TValue value) { if (value == default(TValue)) { ... } } However, the compiler complains that it can't do a == comparison on TValue and TValue. This is the best workaround that I've come up with ...

How is generic function implemented in java?

Hi, As per my understanding the following generic function in java: public static <T> T f(T x) { Integer[] arr = new Integer[4]; T ret = (T) arr[2]; return ret; } is compiled to the following form (as it is unbounded): public static Object f(Object x) { Integer[] arr = new Integer[4]; Object ret = (Object) arr[2]; r...

Why this is not possible in C# Generics?

A colleague pointed me to a strange case in C# (not so sure if this actually strange though). Suppose you have a class Employee. If you want to create a Generic List<> of type Employee, you can simply do: List<Employee> x = new List<Employee>; I understand that I need to pass the Employee type to the Generic list so that it knows t...