generics

how to remove SuppressWarnings("unchecked")

Is there any way I can change this code so I can remove the unchecked warning ArrayList<String> keys = new ArrayList<String>(); // do some stuff then // save keys in session HttpServletRequest request = getThreadLocalRequest(); HttpSession session = request.getSession(true); session.setAttribute(uuid, keys); // get keys from session so...

Why 'Code is not sufficiently generic'?

Can anyone explain why the second example below won't compile? 'Test 2' gives "error FS0670: This code is not sufficiently generic. The type variable ^a could not be generalized because it would escape its scope.". I fail to understand this error message. // Test 1 type test1<'a> = | A of 'a with override t.ToString() = ...

Is it faster the List<T>.Remove(T) or List<T>.RemoveAt(int) method?

Is it faster the List.Remove(T) or List.RemoveAt(int) method in .NET collections? Is speed different for value types or reference types? Thanks, Alberto ...

Generic method call cannot convert from class name to full class name

I have an interface as follows: public interface ISelectEntity { List<T> GetFromDB<T, O>(O data); } I'm implementing it as follows within a StatusCodes class List<StatusCodes> ISelectEntity.GetFromDB<StatusCodes, StatusCodesInputParameters>(StatusCodesInputParameters data) { return EntitiesClass.PopulateStatusCodes(E...

Collection of generic types

If I have a generic class: public class MyClass<T> { public T Value; } I want to instantiate several items such as... new MyClass<string> new MyClass<int> ...and add them to a collection. How do I define the collection so that it can hold a list of generic types? I then want to iterate through the collection at some point, and u...

Comparing two generic lists on a specific property

I'm using VB.NET with .NET 2.0. I have two lists, and I want to compare the lists on a specific property in the object, not the object as a whole, and create a new list that contains objects that are in one list, but not the other. myList1.Add(New Customer(1,"John","Doe") myList1.Add(New Customer(2,"Jane","Doe") myList2.Add(New Cust...

How to reference default IComparer in a custom Dictionary value sort

Suppose I have a method to return a list of keys in a dictionary, sorted by their values: /// Perhaps a wildly inefficient way to achieve this! public static List<K> SortByValues<K,V>( Dictionary<K,V> items ) { var keys = new K[items.Count]; var values = new V[items.Count]; var index = 0; foreach( var kvp in items ) ...

Is this generic autoboxing?

Assigning values without using usual notation like "this.<Double>getAnything(int flag)" private <T> T getAnything(int flag) { Object o = null; if (flag==0) o=new String("NewString"); else if (flag==1) o=new Double(0D); return (T)o; } private void someMethod() { String s = getAnything(0); Double ...

If A<T1,T2> is a template for actual type, then why is typeof(A<,>) allowed?

class Program { static void Main(string[] args) { Type t = typeof(A<,>); Console.WriteLine(typeof(A<,>)); // prints A'2[T1,T2] } } class A<T1,T2> { } As far as I know, generic type A<T1, T2> is not an actual type, but rather a blueprint/template for an actual type, so why does typeof accept it as a par...

gcc template error

why in the first line of this code: template <typename VectorType> std::string repr_vector_dynamic(const VectorType* vect) { std::stringstream strs; strs << "("; for (int i = 0; i < vect->size(); ++i) { if (0 != i) { strs << ", "; } strs << (*vect)[i]; } strs << ")"; return str...

How is IEnumerable<T> Contra-variant ?

This post (http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx) says IEnumerable<T> is Contra-variant. However type T is co-variant because it is an out parameter. So in what context is IEnumerable<T> Contra-variant ?? Hope I am not confusing! Thanks for the answers in advance! ...

List Data Structure C# Efficiency

Hi, at the moment I'm using a List<short> as a buffer to hold things for a while while a calculation is made to each value based on other values further down the buffer. I then realised that this probably wasn't very effecient as I have been told that List<> is a linked list so every time I do whatever = myList[100]; the poor thing is ha...

convert a method into a getter

Hi all, I have a generic method (in a non generic class) returning elements. public IEnumerable<T> GetElements<T>() where T : class { foreach (Element element in elements) { if (element is T) { yield return element as T; } } } I want to transform...

How to get rid of this generics warning?

Hello, I am trying to mock a generic interface and whenever I mock it, I gets this warning The expression of type GenericInterface needs unchecked conversion to conform to GenericInterface My interface is interface GenericInterface<T>{ public T get(); } and My test is @Test public void testGenericMethod(){ Generic...

Using generics with wildcards does not allow using methods with generic as a parameter

If i declare a generic class as something like public class Driver<V extends Car> where Car is an interface. Then, I use it with something like this: Driver<?> driver = new Driver<Chevrolet>(); I don't want to specify a specific implementation of car as the generic. Why is it that I cannot call methods implemented in driver that ...

Could not load type - Custom ViewPage-like baseclass.

I'm working on creating a new base class for an asp.net app in order to create my own version of a strongly typed view from MVC. The app itself is NOT a MVC app though. Below is the basis of my custom class. public class BasePage<T> : Page where T : IPageInfo { public BasePage() { this.MyPageInfo = IoC.GetInstance<IPag...

How to pass generic KeyValuePair<Tkey,TValue> to a WebMethod?

[WebMethod] public void Test(KeyValuePair<string,string> details) { } I have defined the above web-method. The KeyValuePair is defined in http://schemas.datacontract.org/2004/07/System.Collections.Generic how can i add it to my wsdl auto-generated file? i have to define this type because otherwise i get an empty definition...

how to do nested generic classes (if that's the appropriate name) in csharp

I'd like to create a class of the following type public class EnumerableDisposer<IEnumerable<IDisposable>> But it won't let me declare it this way. I've also tried: public class EnumerableDisposer<T> : IDisposable where T : IEnumerable<J> where J : IDisposable But the compiler tells me that the type/namespace J could not be found....

For(typeof(IRepository<>)) in C# to vb ?

I've got a method I'm trying to translate from c# to vb, goes something like this .. x.For(typeof(IRepository<>)).Use(typeof(Repository<>)); VB doesn't seem to like the idea of a IRepository(Of ) ... what's the syntax on that? ...

Generic interface VS generic method?

I'm writing a generic type to handle the underlying Active Directory objects such as groups, organisational units and users. I'm also using "manual" dependency injection within my generic interface. I would like to know if, in my situation, which is more appropriate: generic interface or generic method? Here's a simplified code sample ...