generics

What is the equivalent of java wildcards in C# generics

I'm developing an application where I the need to invoke a method of a generic class and I don't care about the instances actual type. Something like the following Java code: public class Item<T>{ private T item; public doSomething(){...} } ... public void processItems(Item<?>[] items){ for(Item<?> item : items) item.doSomethi...

Java how to: Generic Array creation

Due to the implementation of Java Generics you can't have code like this. How can I implement this while maintaining type safety? public class GenSet<E> { private E a[]; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; } } I saw a solution on the java forums that goes like this: import java.lang.refl...

C# type inference : fails where it shouldn't?

Notice the following code. The offending line has been commented out. interface I<R> { } class C : I<int> { } class Program { private static void function<T, R>(T t) where T : class, I<R> { } static void Main(string[] args) { // function(new C()); // wont compile function<C, int>(new C()); } } ...

How to sort Generic List Asc or Desc ?

I have a generic collection of type MyImageClass, and MyImageClass has an boolean property "IsProfile". I want to sort this generic list which IsProfile == true stands at the start of the list. I have tried this. rptBigImages.DataSource = estate.Images.OrderBy(est=>est.IsProfile).ToList(); with the code above the image stands at the ...

C#: How to reference generic classes and methods in xml documentation

When writing xml documentation you can use <see cref="something">something</see>, which works of course. But how do you reference a class or a method with generic types? public class FancyClass<T> { public string FancyMethod<K>(T value) { return "something fancy"; } } If I was going to write xml documentation somewhere, how would I...

Comparing an instance of a type param to null, even with no class constraint?

The following code compiles: class Testing<TKey, TValue> { public bool Test(TKey key) { return key == null; } } However, TKey can be a value type, and possibly not allow the value "null". I know the results of this program, and how to add constraints. What I'm wondering is why doesn't the compiler disallow this...

Filtering a list

In some cases I've been using a DataTable, filtering it witha DataView and displaying the DataView in a DataGrid. I've recently started switching to using my own classes. For example: [Serializable] [System.Xml.Serialization.XmlRoot("Items", Namespace = "http://mycomp.com/test")] public class Items: List<Item> { } [Serializable] [Sys...

Entity Framework Dynamic Where Clause

I have a query like: var function = GetSomeExpression(); using (FooModel context = new FooModel()) { var bar = context.Bar.Where(function); } I'd like to make a generic method that can execute Where against different Entities in the context. The goal is not having to do context.Bar.Where, context.Car.Where, Context.Far.Where...

How to generic-ize my Dictionary<string, bool> equivalency extension method?

What is the best way to make a generic equivalence function for two Dictionaries, whose keys and values are value types? Right now I have a Dictionary<string, bool>, and have created an extension method that (I think) works to test for equivalence between two Dictionary<string, bool>. I wanted to make it more generic. And my first tho...

How to limit a generic type 's type arguments to a specific type besides using constraints?

I need to specify that a Generic type should only accept enumerated types only in the closed type. Can anyone suggest a way to do this if contraints cannot work? ...

Creating a parametric graph type in Scala

I'd like to create a generic type hierarchy for representing graphs. In particular, I'd like like to have classes Graph and Node, and I want that for every Graph type, there is a corresponding Node type and if I create a generic function for manipulating Graphs, I want this function to use the actual Node type. An example that I tried t...

HOW TO: Get contained type in a Generic List through reflection?

Through reflection, is there some way for me to look at a generic List's contained type to see what type the collection is of? For example: I have a simple set of business objects that derive from an interface, like this: public interface IEntityBase{} public class BusinessEntity : IEntityBase { public IList<string> SomeString...

Casting a generic collection to base type

I've got an IList<DerivedClass> that I want to cast to ICollection<BaseClass> but when I attempt an explicit cast, I get null. Is it possible to do this without creating and populating a new collection? Edit: Since I only want to read from the collection, I switched to using a generic method: public void PopulateList<BaseClass>(IColle...

Abstract Class - Am I over-thinking this or doing it right?

So I am utilizing CollectionBase as an inherited class for custom collections. I am utilizing CollectionBase through an abstract class so that I don't repeated knowledge (following the DRY principle). The abstract class is defined as a generic class also. Here is how I am implementing my class: public abstract class GenericCollecti...

Custom Class for dealing with embedding in Forms

I have a custom class file in C# that I inherited and partially extended. I am trying to re factor it now as I have just enough knowhow to know that with something like generics(I think) I could greatly condense this class. As an inexperienced solo dev I would greatly appreciate any direction or constructive critism any can provide. ...

Why cant the compiler infer the type for me? (aka Smarter SmartEnumerable)

I am using Jon Skeet's very clever SmartEnumerable. I recommend checking it out if you haven't already seen it. The class is defined : public class SmartEnumerable<T> : IEnumerable<SmartEnumerable<T>.Entry> The constructor is : public SmartEnumerable(IEnumerable<T> enumerable) and you use it by saying : new SmartEnumerable<Cat>(m...

DataTable to Generic List

public static IList<T> ConvertTo<T>(DataTable table) { if (table == null) { return null; } List<DataRow> rows = new List<DataRow>(); foreach (DataRow row in table.Rows) { rows.Add(row); } return ConvertTo<T>(rows); } public static ...

Combination of List<List<int>>

I've a List of this type List> that contains this List<int> A = new List<int> {1, 2, 3, 4, 5}; List<int> B = new List<int> {0, 1}; List<int> C = new List<int> {6}; List<int> X = new List<int> {....,....}; I want to have all combinations like this 1-0-6 1-1-6 2-0-6 2-1-6 3-0-6 and so on. According to you is This possibile to resolv...

Search, Filter AND Remove a List<List<T>>.

I've a problem! I've a List <List<Class>> L = new List<List<Class>>(); where Class contains IDLinea Posizione And so on... I want to have a Distinct List<List<Class>> Where there are no objects with same lines. For example: List <List<Class>> L = new List<List<Class>>(); var A = new List<Class>(); Class C = new Class(); C.ID...

How do I code these generic functions and classes inheritances correctly? (VB .NET)

I have a function which I need to call for three different types, with the underlying logic remaining the same for all the different types, so I figured it would be best to write this function using generics. Here is the basic outline of the classes and functions involved: 'PO Base class' Public MustInherit Class ProductionOrder P...