generics

What is the meaning of the type safety warning in certain Java generics casts?

What is the meaning of the Java warning "Type safety: The cast from Object to List is actually checking against the erased type List"? I get it when I try to cast an Object to a type with generic information, such as in the following code: Object object = getMyList();List<Integer> list = (List<Integer>) object;...

In C#, why can't a List<string> object be stored in a List<object> variable

It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way. List<string> sl = new List<string>();List<object> ol;ol = sl; results in Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.Generic.List’ And then... List<string> sl = new List<string...

Generic Type Conversion FROM String

Hi Guys, Here is the problem I am having, I have a class that I want to use to store "properties" for another class, these properties simply have a name and a value. Ideally, what I would like is to be able to add typed properties, so that the "value" returned is always of the type that I want it to be. The type should always be a pr...

Generic Type Checking

OK, here we go again! Following on from my previous question on Converting to a Generic Type from String, I thought I would ask another Generics-related question! This time, is there a way to enforce/limit the types that are passed to PRIMITIVE's? (bool, int, string, etc) Now, I know you can limit the generic type parameter to a type o...

Generic iterator

I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list. The custom list defines an iterator; class Iterator: public std::iterator<std::forward_iterator_tag, T> { // ... } Iterator begin() { return (Iterator(root)); } Iterator end() { return ...

How can I override an EJB 3 session bean method with a generic argument - if possible at all?

Suppose you have the following EJB 3 interfaces/classes: public interface Repository<E> { public void delete(E entity); } public abstract class AbstractRepository<E> implements Repository<E> { public void delete(E entity){ //... } } public interface FooRepository<Foo> { //other methods } @Local(FooRepository....

What is the best way to iterate through a strongly-typed generic List<T>?

What is the best way to iterate through a strongly-typed generic List in C#.NET and VB.NET? ...

Sorting an IList in C#

So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it. Turns out the IList interface doesn't have a sort method built in. I ended up using the ArrayList.Adapter(list).Sort(new MyComparer()) method to solve the problem but it just seemed a bit "gh...

Generics in c# & accessing the static members of T

Hi there! My question concerns c# and how to access Static memebers ... Well I don't really know how to explain it (wich kind of is bad for a question isn't it?) I will just give you some sample code: Class test<T>{ int method1(Obj Parameter1){ //in here I want to do something which I would explain as T.TryParse(...

List<BusinessObject> or BusinessObjectCollection?

Prior to C# generics, everyone would code collections for their business objects by creating a collection base that implemented IEnumerable IE: public class CollectionBase : IEnumerable and then would derive their Business Object collections from that. public class BusinessObjectCollection : CollectionBase Now with the generic lis...

Why can't I explicitly pass the type argument to a generic Java method?

I have defined a Java function: static <T> List<T> createEmptyList() { return new ArrayList<T>(); } One way to call it is like so: List<Integer> myList = createEmptyList(); // Compiles Why can't I call it by explicitly passing the generic type argument? : Object myObject = createEmtpyList<Integer>(); // Doesn't compile. Why? ...

Best way to test if a generic type is a string? (c#)

I have a generic class that should allow any type, primitive or otherwise. The only problem with this is using default(T). When you call default on a value type or a string, it initializes it to a reasonable value (such as empty string). When you call default(T) on an object, it returns null. For various reasons we need to ensure that if...

What are the differences between Generics in C# and Java... and Templates in C++?

I mostly use Java and generics are relatively new. I keep reading that Java made the wrong decision or that .NET has better implementations etc. etc. So, what are the main differences between C++, C#, Java in generics? Pros/cons of each? ...

How can I convert IEnumerable<T> to List<T> in C#?

I am using LINQ to query a generic dictionary and then use the result as the datasource for my ListView (WebForms). Simplified code: Dictionary<Guid, Record> dict = GetAllRecords(); myListView.DataSource = dict.Values.Where(rec => rec.Name == "foo"); myListView.DataBind(); I thought that would work but in fact it throws a System.Inva...

C# generic constraint for only integers

Can anyone tell me if there is a way with c# generics to limit a type T to only Int16, Int32, Int64, UInt16, UInt32, UInt64 I'm aware of the where keyword, but can't find an interface for only these types, Something like: static bool IntegerFunction<T>(T value) where T : INumeric Thanks ...

.Net 2.0 - How efficient are Generic Lists?

I'm creating an app that holds loads of loads of user data in memory, and it's mostly keeping it all in List<T> structures (and some Dictionary<T,T> when I need lookup). And I'm wondering... How efficient are Lists? How much memory overhead do I get for each of them? (that is, memory space in addition to what the objects they contain w...

What are the differences between "generic" types in C++ and Java?

Java has the generic keyword and C++ provides a very strong programming model with templates. So then, what is the difference between C++ and Java generics? ...

Generics in Java

Is there any shorthand way of defining and using generic definations without having to keep repeating a particular generic description such that if there is a change I don't have to change all definations/usages though out the codebase for example is somethhing like this possible: Typedef myGenDef = < Object1, Object2 >; HashMap< myGen...

Asking a Generic Method to Throw Specific Exception Type on FAIL

Right, I know I am totally going to look an idiot with this one, but my brain is just not kicking in to gear this morning. I want to have a method where I can say "if it goes bad, come back with this type of Exception", right? For example, something like (and this doesn't work): static ExType TestException<ExType>(string message) ...

Cast List<int> to List<string>

Does anyone know if you can cast a List<int> to List<string> somehow? I know I could loop through and .ToString() the thing but a cast would be awesome. I'm in c# 2.0 (so no linq) ...