generics

f# generic type comparison

I'm trying to figure out if an obj returned from a call is of a certain type. Here is my code: type MyType<'T>= val mutable myArr : array val mutable id : int val mutable value : 'T and in some method that has MyType in scope... let a = someFunThatReturnsObj() // a could be of type MyType How do I figure out if a ...

How would you convert between List<Object> -> ObjectList and ObjectList -> List(Object) using generics

I find that I need to write the same code in many classes. The code has to do with conversion between types. As I am not familiar with generics, would someone suggest how to convert the following to use generics: public class WidgetList : List<Widget> { } public class Widget { public int Id { get; set; } public string Name { g...

Strongly-typed generic method invokes its argument's base class method, instead of a shadowed method in T?

Consider a MyForm class that contains a shadowed implementation of Show(), and a CreateForm() method that accepts an instance of the form and calls the shadowed sub: Public Class MyForm Inherits Form Public Shadows Sub Show() MessageBox.Show("Shadowed implementation called!") End Sub End Class ... Public Sub Creat...

Referencing a parameterized type from an annotation

I am trying to figure out how to reference a parameterized interface as an annotation attribute: public class Example { public interface MyService<T extends Number> { T someNumber(); } public class BaseServiceImpl<T extends Number> implements MyService<T> { @Override public T someNumber() { ...

Array of Generic Interface

Can we create array of generic interface in java? interface Sample<T>{} in other class Sample<T> s[] = new Sample[2] ; // for this it shows warning Sample<T> s[] = new Sample<T>[2];// for this it shows error ...

LINQ Query with OrderBy in object that inherits from KeyedCollection not using IEnumerableOf

I have this class declaration: Public Class clsColection(Of T) Inherits System.Collections.ObjectModel.KeyedCollection(Of String, T) Implements System.Collections.IEnumerable Implements System.Collections.Generic.IEnumerable(Of T) ... Public Shadows Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T...

Generic List Method Problem

Hello, I'm getting an error when I try to create a method with the following signature: public List<T> CreateList(DataSet dataset) Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) Does anyone know what I'm doing wrong? Thanks in advance! ...

How to use compound key for dictionary?

Hi I need to arrange sort of dictionary where the key would be a pair of enum and int and value is object. So I want to map a pair to some object. One option would be public enum SomeEnum { value1, value2 } class Key { public SomeEnum; public int counter; // Do I have to implement Compare here? } Dictionary<SomeEnum, obje...

A question regarding c# interfaces and maybe generics

Hi, We have built an internal tool that generates the whole data access, each table has a class the represents it's data and all the common operations.(think lightweight Entity framework). These DataAccess objects always have a constructor that receives a connection string, and a Load function that receives a SqlDataReader. Something ...

Grouping KeyValue pairs to Dictionary

Hi, I have the following code : using System.Collections.Generic; public class Test { static void Main() { var items = new List<KeyValuePair<int, User>> { new KeyValuePair<int, User>(1, new User {FirstName = "Name1"}), new KeyValuePair<in...

Grouping Nested KeyValue pairs to Dictionary

Hi, I have the following code: using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; public class Test { static void Main() { var list = new List<KeyValuePair<int, KeyValuePair<int, User>>> { new KeyValuePair<int, K...

How to chain method overloads that accept Func<> delegates ? (C#)

I have a method with two overloads, as follows: bool Evaluate(Func<bool> condition) { // Some logic return condition.Invoke(); } bool Evaluate<T>(Func<T, bool> condition, T value) { // Same logic as the first method overload return condition.Invoke(value); } Since both method overloads contain largely identical logi...

C# casting from list of generic types

We have the following interface that we use to define how an entity should be indexed (using lucene): public interface IIndexDefinition<T> where T : IIndexable { Document Convert(T entity); } This means we can keep our index service very simple. So to index an entity we have: IndexResult IndexEntities<TEntity>(IEnumerable<TEnt...

How do you convert a DataSet/DataTable into a generic list of certain type?

I have looked at the answered topic with the answer: List list = dt.AsEnumerable().ToList(); However, what if wanted to convert to a list of some entity type? For instance, I have an entity class Property: public class Property { public int ID { get; set; } public string Chain { get; set; } public string Na...

Get value from Dictionary<string, object> without unboxing?

Hey, I was wondering if it's possible to run the following code but without the unboxing line:- t.Value = (T)x; Or maybe if there is another way to do this kind of operation? Here is the full code:- public class ValueWrapper<T> { public T Value { get; set; } public bool HasValue { get; set; } public ValueWrapper() ...

Taking distinct item from list of datapoints

I have a list of Datapoints (List). e.g original list (1,2) (2,2) (1,3) (3,3) (4,3) (2,3) (5,4) I want a output list as (1,2) (2,2) (3,3) (4,3) (5,4) or (1,3) (3,3) (4,3) (2,3) (5,4) i.e I want to remove all the other points where X value duplicates. One approach that I have is to loop through all the point and take the current p...

Java Generics - implements and extends

Hi, I am trying to write something like public class A implements B<T implements C> {} and public abstract class M<I extends P> extends R<I extends P> implements Q<I extends P> {} but I am getting errors like Multiple markers and syntax error on token extends, expected. Please let me know what is the correct way of doing this. ...

Java generics (template) specialization possible (overriding template types with specific types)

I'm wondering what are the options to specialize generic types in Java, i.e. in a templated class to have specific overrides for certain types. In my case I was a generic class (of type T) to return null usually, but return "" (the empty string), when T is the String type, or 0 (zero) when its the Integer type, etc. Merely providing a ...

Method to return a generic type when passing in a delegate as a parameter

I'm trying to 'genericize' some code we have spattered around our system. I want to: return a generic type, pass in some kind of delegate containing the method to be called. I'm pretty new to generics so any help appreciated. Below is where my finger in the air is(!) public static T ReturnSingleObject<T>(Func<string, int, T> dynam...

generic class constraints: 2 types

i want to create a generic class that would accept T. T is an object coming from Entity Framework representing a Table, or a View of that table. properties on both would be the same. I would like to create a generic class, that will accept the table or view, and construct a linq query based on the properties. so i would need to d...