If one calls the .Max() extension method on an IEnumerable<T>, and the objects within do not implement IComparable, one gets System.ArgumentException: At least one object must implement IComparable.
Why don't Max and similar methods constrain T to implement IComparable, so that this problem can be caught at compile time instead of at ru...
bool IsTypeAGenericList(Type listType)
{
typeof(IList<>).IsAssignableFrom(listType.GetGenericTypeDefinition())
}
returns false when given typeof(List<int>).
I assume this is because the two type parameters can be different, correct?
...
Why can I do this:
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return (T)GetMainContentItem(moduleKey, itemKey);
}
but not this:
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return GetMainContentItem(moduleKey, itemKey) as T;
}
It complains that I haven't restricted the generic t...
I have an interface
interface x {
A getValue();
}
and the implementation
class y implements x {
public B getValue() { return new B();}
}
B is a subclass of A. This works because of covariant overriding, I guess.
But if I rewrite the interface as
interface x{
<T extends A> T getValue();
}
I get a warning in the i...
Duplicate
In C#, why can’t a List object be stored in a List variable
Here is my code:
public class Base
{
protected BindingList<SampleBase> m_samples;
public Base() { }
}
public class Derived : Base
{
public Derived()
{
m_samples = new BindingList<SampleDerived>();...
Possible Duplicate:
Is there a Set data structure in .Net?
Duplicate: this is a duplicate of "Is there a “Set” data structure in .Net?". Please close it as a duplicate and address any further answers to the earlier question.
Is there a generic collection analogous to the STL set<T> template in the .NET framework?
If not, how w...
How do Concepts (ie those recently dropped from the C++0x standard) differ from Interfaces in languages such as Java?
...
Suppose I have two classes, Input and Output, which are designed to be connected to each other. Output produces values of some type, and Input consumes them.
class Input[T] {
var output: Option[Output[_ <: T]] = None
}
class Output[T] {
var input: Option[Input[_ >: T]] = None
}
It's okay if an Input and Output pair don't operate o...
I would like to be able to detirmine the return type of my method call at runtime, but I can not seem to be able to get the Type of T.
public <T> T getT()
{
Object t = null;
Class<?> c = t.getClass();
System.out.println(c.getName());
return (T) t;
}
Is there any way to determine the Type of T at runtime in Java?
...
I have a collection class with an Equals method that I want to pass in a method to do equality checking between each item. Furthermore, I want to allow the delegate type to operate on superclasses of T as well as T itself:
public delegate bool EqualityComparer<T>(T x, T y);
public class Collection<T>
{
//...
public bool Equals...
Hi I want to create my custom collection, I am deriving my custom collection class from CollectionBase class as below:
public class MyCollection : System.Collectio.CollectionBase
{
MyCollection(){}
public void Add(MyClass item)
{
this.List.Add(item);
}
}
class MyClass
{
public string name;
}
Let me ask a f...
Which type cannot be used as generics?
...
This question got me thinking on how one could approach writing a method that would need to contain a variable list of type parameters.
One approach would be to accept params Type[] as part of the arguments, such as:
public static bool IsOneOf(this object obj, params Type[] types)
{
return types.Contains(obj.GetType());
}
Howeve...
Hello,
I have a generic method
public static void DoSomething<T>()
{...}
. Now I want to restrict that T.
public static void DoSomething<T>() where T: IInterface1
{...}
But what I really want is allowing multiple interfaces, something like
public static void DoSomething<T>() where T: IInterface1, IInterface2
{...}
But that does...
I have a class Foo with subclass SubFoo, a collection class FooListing.
I want to create a generic FooListing<T> where T is either Foo or a subclass of Foo
From the wording of the documentation (under 'Wildcards'), it sounds like
public class FooListing<T extends Foo> { ... }
...would let me use FooListing<SubFoo>, but not FooListin...
Hello good people! in my long journey to learn about hibernate, i wanted to use generic DAO and came across a good article at the hibernate site and tried out the IMPLEMENTATION WITH HIBERNATE Section.I'm having an error saying :GenericDAOImpl.java:[22,16] name clash: makeTransient(T) in GenericDAOImpl and makeTransient(T) in GenericDAO ...
I am trying to derive the type of an object at runtime. Specifically I need to know two things whether it implements ICollection or IDto. Currently my only solution I have been able to find is this:
private static bool IsACollection(PropertyDescriptor descriptor)
{
bool isCollection = false;
foreach (Type type ...
What I want to do is, based on the type of T do different opperations. Below is a simple example of my problem.
Public Shared Function Example(Of T)() As T
Dim retval As T
If TypeOf retval Is String Then
Dim myString As String = "Hello"
retval = myString
ElseIf TypeOf retval Is Integer Then
Dim myInt...
When building my application in release mode, I get an internal error URW1135 at the end of one of my files. I searched the internet and found (here) that it could be about arrays of generic types like
MyClass <T> = class
FArray : array of T;
end;
So, I applied the proposed workaround (declaring a type TArrayType = array of T) throu...
I have problems to get generics to work in the following scenario:
Delphi provides the interface IComparable:
IComparable <T> = interface
function CompareTo (Value : T) : Integer;
end;
I add another interface IPersistent:
IPersistent = interface
function ToString : String;
procedure FromString (const Str : String);
end;
One...