Some special CLI types from mscorlib library (ArgIterator, TypedReference and RuntimeArgumentHandle types) cannot be used as generic type parameters to construct the generic types / methods:
void Foo<T>() { }
void Bar() { Foo<ArgIterator>(); }
provides the compiler error:
error CS0306: The type 'System.ArgIterator' may not be used as...
Before using google collections I had something similar to next code:
private Set<A> aSet = ...;
private Set<B> bSet = ...;
public Foo getFoo (Map<?, List<Bar>> bars, Set<?> set) {
for (Object item : set) {
for (Bar bar : bars.get (item)) {
//build foo;
}
}
...
}
and I was able to make calls like these:...
I am trying to use the answer of a preceding question to implement a small graph library. The idea is to consider graphs as colections, where vertices wrap collection elements.
I would like to use abstract types to represent Vertex and Edge types (because of type safety) and I want to use type parameters to represent the type of the col...
How do I Load the class "MyContent" dynamically ?
I have 1 interface<T>, 1 abstract generic class<T> and 1 class. Check my code out:
public interface IMyObjectInterface{
}
public abstract MyAbstractObject : IMyObjectInterface{
}
public class MyObject : MyAbstractObject{
}
public interface IMyContentInterface<T> where T : MyAbstractObj...
I have a problem where I need a .NET dictionary that supports multiple items per key. In the past I've used the STL multimap in my C++ programs. How does the design of a multimap differ from a dictionary of lists i.e. performance, size, etc. (excluding generics vs. templates)?
...
I have the following extension method that takes a List and converts it to a comma separated string:
static public string ToCsv(this List<string> lst)
{
const string SEPARATOR = ", ";
string csv = string.Empty;
foreach (var item in lst)
csv += item + SEPARATOR;
// remove the trailing...
Is there any way to express this idea in C#? (Basically a generic type of a generic type)?
public static class ExtensionSpike
{
public static IEnumerable<T> Where<TCollection<T>>(this TCollection<T> sourceCollection, Expression<Func<T, bool>> expr)
where TCollection : class, IEnumerable<T>, INotifyCollectionChanged
{
...
I have a base class, with a method, where I would like to use generics to force the coder to use a generic expression on the current class:
public class TestClass
{
public void DoStuffWithFuncRef<T>(Expression<Func<T, object>> expression) where T : TestClass
{
this.DoStuffWithFuncRef(Property<T>.NameFor(expressio...
Suppose you write a static function in Java to sort an array, much like Arrays.sort(). The problem with Arrays.sort() is that it receives an array of Object, and throws a ClassCastException if its elements don't implement Comparable.
So you want your function to receive as an argument an array of a subtype of Comparable. Something like ...
There's a few questions on SO about StructureMap and generics, and I've read a few blog posts about it, but still I'm struggling to figure out the solution to this particular scenario.
Given:
public interface ILookup
{
}
public interface ISupplier : ILookup
{
}
public interface ITenant : ILookup
{
}
public class Supplier : ISupplier...
Is there a way to specify that a generic type be of one type or another type?
public class SoftDrink<T>
where T : TypeOne or TypeTwo
{ }
...
Hi,
I have an ArrayList of objects that I need to sort in two different fashions, depending on the situation. I followed this example: http://codebetter.com/blogs/david.hayden/archive/2005/03/06/56584.aspx on how to create a PersonComparer by overloading the IComparer object. I liked this method because it allowed me to build an enum...
I'm using Hibernate validator and trying to create a little util class:
public class DataRecordValidator<T> {
public void validate(Class<T> clazz, T validateMe) {
ClassValidator<T> validator = new ClassValidator<T>(clazz);
InvalidValue[] errors = validator.getInvalidValues(validateMe);
[...]
}
}
Questio...
Here is an abstraction and simplification of my issue:
I have a set of toys and a corresponding box for these toys. I want the user to be able to specify the largest type of toy that the box can hold:
public class Box<T> {}
then within the Box class I want to have a generic list of toys, but each toy contained within the box will hav...
Is it possible to achieve the following code? I know it doesn't work, but I'm wondering if there is a workaround?
Type k = typeof(double);
List<k> lst = new List<k>();
...
I have code like this:
Type typPrecise = MostPrecise(typeof(int), typeof(double));//Evaluates to double
var varGeneric = typeof(Number<>);
var varSpecific = varGeneric.MakeGenericType(typPrecise);
dynamic nmNumber = Activator.CreateInstance(varSpecific);
The nmNumber is of dynamic type and essentially produces a Generic Number. How ...
I have a class like this:
public class Foo<T> : IEquatable<T> where T : struct
{
List<T> lst;
[Other irrelevant member stuff]
}
I want to implement the IEquatable<T> interface for the Foo class. What do I need to do. For simplicity I want to just check whether the List members are equal.
Thanks.
C# 4.0 supported answers ar...
Hi folks.
First of all I'm only aware of Java basics. Now I have the following scenario:
If have a generic class:
public class ListObject<T>
{
// fields
protected T _Value = null;
// ..
}
Now I want to do something like the following:
ListObject<MyClass> foo = new ListObject<MyClass>();
ListObject<MyClass> foo2 = new L...
I have a class:
[Serializable]
public class KVPair<TKey, TValue>
{
public TKey Key { get; set; }
public TValue Value { get; set; }
public KVPair(TKey k, TValue v)
{
Key = k;
Value = v;
}
}
that I create:
List<KVPair<string,string>> kvPairs;
Using the J...
I have a List<int> and I want to convert it to a List<double>. Is there any way to do this other than just looping through the List<int> and adding to a new List<double> like so:
List<int> lstInt = new List<int>(new int[] {1,2,3});
List<double> lstDouble = new List<double>(lstInt.Count);//Either Count or Length, I don't remember
for (i...