generics

problem with generics delegate

Hi All, I am new to c#. I am trying to declare a delegate function which takes 2 generics inputs. I am having problems compile it. can anyone tell me what is the problem here. delegate int ippcFuncPtr<in T1, in T2>(T1 param, T2 returnval); static int ippcServerRegisterProcedure(int handle, string procedureName, ippcFuncPtr<in T1,...

Generic in type constraint

I'm struggling with some generics. The following is my setup: interface I<T> { } [...] void Add<T>(T obj) where T : I<??> { } How can I ensure that T in the Add method implements I? ...

class derivation problem

I have the following 3 classes: class Node { public Node Parent; // Edit: to clarify, each of these classes has many fields and methods public void Method1() { /*...*/ } public void Method2() { /*...*/ } /* ... */ public void Method30() { /*...*/ } } class Element : Node { public Dictionary<string, string> Attri...

Raw types, unbounded wilcard and bounded wildcard

I have a quick question as below: Here's a simple examples about this whole issues: List a = new ArrayList(); List <?> b; List <? extends Object> c; According to Java SCJP by khalid mughal (a very good book!): a = b; // ok. Widening conversion. b = a; // ok too. No unchecked warning. b = c; // ok c = b; // ok c=a; // ok but now wil...

using free bound generic type as type parameter

Hi, Do you see a way to specify that my result type have to be MonadType< arg type > within this interface ? interface IMonad<MonadType> // where MonadType : GenricType<> { MonadType<T1> unit<T1>(T1 t) Func<MonadType<T1>, MonadType<T2>> map<T1, T2>(Func<T1, T2> f); } I get as an error : The type parameter 'MonadType' cannot...

Data Access Layer - LINQ-To-SQL and generics. Can I optimize this?

Hi, We are working on improving our DAL which is written in LINQ that talks to the MS SQL database. Our goal is to achieve good re-usability with as little code as possible. LINQ generated files are making a use of generics and reflection to map LINQ generated classes to the SQL objects (tables and views in our case). Please see the e...

Generic question... Calling a static method from a generic class...

Hi! I have a generic class: public class MyList<LinkedItem> : List<LinkedItem> where LinkedItem : MyItem, new() { } From that generic class, I would like to access a static function from the LinkedItem Class which is a descendant of MyItem class. (thus without creating an instance of the LinkedItem). Is it possible? Thank you, Eri...

Why null was called "a subtype of every reference type" ?

Guys, could any one give a logical explanation of phrase I met in this book: You may find it helpful to think of ? extends T as containing every type in an interval bounded by the type of null below and by T above (where the type of null is a subtype of every reference type). Thanks. ...

Generics in Java

Having considered the official Java tutorial and some threads from the past, few things remain unclear and so I'd be glad if someone would explain it to be in plain words. Class valueClass = agp.getValueClass(); ///the method's return type is Class<?> if(Double.class.equals(valueClass)) { ... } With that my IDE shows me a raw type wa...

F#: explicit type parameters in operator binding

I'm trying to define operator with the explicit type parameters and constraints: let inline (===)<'a, 'b when 'a : not struct and 'b : not struct> a b = obj.ReferenceEquals (a,b) It works well in F# 2.0, but produces the: warning FS1189: Type parameters must be placed directly adjacent to the type name, e.g. "type C<'...

How to convert datatable into generic type.

How to convert DataTable having N rows into KeyValuePair<long,string>[] type having same N no of rows I want to return KeyValuePair<long,string>[] kind of value from a function that will convert DataTable into above type. ...

Generic ServiceContract

[ServiceContract] public interface ISecurities<T> : IPolicyProvider where T: EntityObject { [OperationContract(Name="GetAllSecurities")] IEnumerable<T> GetSecurities(); [OperationContract] IEnumerable<T> GetSecurities<T1>(List<T1> lstIdentifiers) where T1 : FI_CusipMaster; [OperationContract] T GetSecurity<T1>(T1 lstIden...

how to use linq on a list<T> to generate a list<lists<T>> in C#

I've seen examples of the reverse question, but they don't help me with this direction. I've got a List where T has an int weight. I'd like to divide this into lists of 5 grouped by weight. if I have the following T's with respective weights A:1 B:2 C:3 D:4 E:5 F:6 G:7 H:8 I:9 J:-11 I want {A,B,C,D,E,F,G,H,I,J} to be sorted to {{J,A,B,...

Splitting a C# List<T> Into Two

I'm using C# and ASP.NET 3.5. Basically I'm retrieving a column of data from a dataset and putting this into a list like so: List<String> dates = new List<String>(); foreach (DataRow rowMonth in myDS.Tables[0].Rows) { string ListedMonthYear = (string)rowMonth[0]; dates.Add(ListedMonthYear); } The retur...

How I can get a instance by a class?

I have a method public static <T> T createObject(Class<T> t){ } You see, I want to get a instance of T. I know this can be realized, because: public <T> T find(Object id, Class<T> type) { return (T) this.em.find(type, id); } Help me........ ...

How can I learn actual type argument of an generic class?

Hi, I have a parameterized class : class ParameterizedClass<T extends AbstractSomething> { } calling: new ParameterizedClass<Something>(); So how can I get actual type Something of T by using Java Generics? ...

Getting error while executing Ienumerable<Datarow>.Sum() method.

Why isn't the below code working? I am getting an error. It says 'Cannot implicitly convert type 'V' to int ' . private ObservableCollection<DataItem> method<T, V>(DataTable dt, SeriesItem seriesItem, FilterValues filter, ObservableCollection<DataItem> chart) { var result = from t in dt.AsEnumerable() ...

Generics and Extension Methods Together.

Hi I need to create an extension method to array class, but this extension method must be able to accept many data types, so it also must be generic. In the code bellow the extension method just accept byte data type. I want it to also accept ushort and uint for instance. I believe that the best way to do that is creating a generic typ...

C# Collection using Generic types or Interface

Hi All, I have a set of classes (MyClass1, MyClass2, MyClass3) that implement an interface (IMyClass). I am interested in creating a method to fill and return a list (Collection.Generics.Lists<>) of objects that have common interface MyClassX (List<IMyClass>). But how? Each class has a constructor MyClassX which does something differe...

Should generic constraints be preferred to using interfaces as parameter types?

Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0; } Now, if I pass an int to this method, it gets boxed. Wouldn't it therefore be better to define the above method as follows? public static bool IsPositive<T>(T value) where T : IComparable<int> { return valu...