generics

Generics and inheritance in C#

I have entity class generated by E-R designer that I have modified a little. This is declaration: public abstract partial class Preference<T> : EntityObject, IPreference<T> Then there is another entity class declared as follows: public partial class BoolPref : Preference<bool> so BoolPref inherits from Preferences<bool>. Now I have...

Generics and casting - cannot cast inherited class to base class

I know this is old, yet I am still not very good with understanding those problems. Can anyone tell me why the following does not work (throws a runtime exception about casting)? public abstract class EntityBase { } public class MyEntity : EntityBase { } public abstract class RepositoryBase<T> where T : EntityBase { } public class MyEn...

Whats the best way to convert a datarow[] to List<int>?

I want to convert a datarow[] into a List<int> List<int> UserIds; public void GetGravatars() { var cmd = access.GetSqlCommandStoredProcedure("uspMembershipGetUserGravatarList"); var table = access.ExecuteDataTable(cmd, DAL.Logic.Common.ConnectionStrings.User); int[] rows = new int[table.Rows.Count]; ...

What is the quickest generic collection?

I have the following scenario. I have two collections, one has some items missing. What collection type would be the quickest to find the missing items and insert them? ...

implicit or explicit conversion from T to T[]

Is there a way to implement a generic implicit or explicit converter for anything to an array of anything, something like this: public static implicit operator T[](T objToConvert) { return new T[] { objToConvert }; } ...

Design Advice - How to convert these methods to harness C# Generics

I have throughout my application many methods where I load collections. They all (actually most with a couple of differentials) follow the following pattern: public BaseCollection<ObjectType1> LoadObjectType1(EventHandler handleEvent) { var myQuery = ObjectType1.Load(MyServiceContext); return new DataManager<ObjectType1>().GetD...

Is there a way in scala to produce a generic instance without an example instance?

I was playing with creating a generic factory as follows: trait Factory[T] { def createInstance():T = new T() } val dateFactory = new Factory[Date](){} val myDate = dateFactory.createInstance() The 'new T()' doesn't compile, as T is undefined until runtime. I know that I can get it to work by passing in an instance of the class to so...

vb.net compare generic types

I have a class which I use to record a value in a given interval. I.e. the value between 0 and 1 might be 0.5, 1 to 5 might be 1, and 5 to 100 might be 5. However I would like to have this information stored in a class that uses generics. So when I try to find out the value in a certain interval I would like to be able to call on a fun...

Is it possible to have C++-like generics in C#?

Hi guys, Perhaps this question was already asked million times but I couldn't find similar topic. Is it possible to write a generic class with multiple 'where'-s that will be checked in compile time? Here is what I have today public class MsBitsEnumWrapper<TC, TE> : IEnumerable<TC> { internal class Helper : IEnumerator<TC> { ...

Two separate generic types have the same erasure?

I stumbled upon an interesting error that I've never seen before, and can't explain why Consider the following class public class Sandbox<A,B> { public void put(B b) { } public void put(A a) { } } Looks okay to my eyes. So I compile it and then get this name clash: put(B) and put(A) have the same erasure Huh? How do two differ...

C#: How do i make sense of generics like Action<IEnumerable<Customer>, Exception>

i am new to c#. coming from mainly PHP background or basic VB. i understand that IEnumerable<Customer> is something like a list of customer. but what is Action<IEnumerable<Customer>, Exception>. an action of type IEnumerable<Customer> & Exception. doesnt seem to make too much sense to me. ...

Java: Generics syntax

This works to return a list of ints: public List<Integer> GetIListImpl() { return new ArrayList<Integer>(); } But what if I want to let the caller specify the generic type? Something like this, although syntactically I'm not sure how to do it: public List<T> GetIListImpl<T>() { return new ArrayList<T>(); } The usage would b...

Isn't it easier to work with foo when it is represented by the class ArrayList rather than the interface List?

I see this syntax a lot and don't understand the reasoning behind it. I thought you generally want to work with classes rather than interfaces to make it easier to carry out the full panoply of operations you might want to perform. Why do this: List<Foo> foo = new ArrayList<Foo>(something.getFoo()); instead of this: ArrayList<Foo> f...

Raw to <? extends Object>

I just don't understand this. List list = new ArrayList(); List <? extends Object> list1 = list; // unchecked conversion warning. Since Object is the highest upper bound in java, i don't see no reasons why the warning is there. Please advice ...

Create a zero value of a generic Number subclass

How can I generically create a zero of an arbitrary numeric type? Here's a toy example: a function that converts a null number into zero. static <T extends Number> T zeroIfNull(T value) { return value == null ? 0 : value; } This doesn't compile because the literal zero is of type int, and I need to convert that to type T. Is it ...

reason generics were introduced in java 5

Can someone explain what were the reasons generics were introduced in Java? As far as I understand they were introduced so that you don't accidentally add the wrong type of object to a collection. I kind of like the generics for collection but can't see why they were added for other classes too. I have seen some generic classes that m...

How to invoke System.Linq.Enumerable.Count<> on IEnumerable<T> using Reflection?

I have a bunch of IEnumerable Collections which exact number and types is subject of frequent changes (due to automatic code generation). It looks something like this: public class MyCollections { public System.Collections.Generic.IEnumerable<SomeType> SomeTypeCollection; public System.Collections.Generic.IEnumerable<OtherType>...

Multiple wildcards on a generic methods makes Java compiler (and me!) very confused

Let's first consider a simple scenario (see complete source on ideone.com): import java.util.*; public class TwoListsOfUnknowns { static void doNothing(List<?> list1, List<?> list2) { } public static void main(String[] args) { List<String> list1 = null; List<Integer> list2 = null; doNothing(list1, list2...

How to get IEnumerable<T> from List<T> ?

Hello. I have a List and I need an IEnumerable, however List.GetEnumerator() returns List.Enumerator ... Is there a simple way of getting (casting to?) an IEnumerator? (currently I have solved this with a loop, however I feel casting the enumerator would be a far better solution)... ...

How do I convert from List<?> to List<T> in Java using generics?

In Java, how do I convert List<?> to List<T> using a general purpose method so that I can replace patterns like the following with a single method call: List untypedList = new ArrayList(); // or returned from a legacy method List<Integer> typedList = new ArrayList<Integer>(); for (Object item: untypedList) typedList.add((Integer)it...