generics

split generic list

I need to split a list into two equal lists. For Example: I have a list which consists of 10 items. I need to split the list into two equal parts(each with 5 items) I have a list which consists of 9 items sometimes. I need to split the list into two parts(one with 5 items and other with 4 items) Please suggest a solution for this. ...

Generics, abstract classes, constructors

I have an abstract base class class AbstractClass { Col<AbstractClass> parent public AbstractClass() { //do stuff } } I have two implementations class A : AbstractClass { Col<A> parent public A(Col<A> parent) :base(parent) { this.parent = parent; } } class B : AbstractC...

I'm studying Head First Java, but I can't understand Page 544

"When you declare a type parameter for the class, you can simply use that type any place that you'd use a real class or interface type. The type declared in the method argument is essentially replaced with the type you use when you instantiate the class. If the class itself doesn't use a type parameter, you can still specify one for a m...

Howto Generalize SSCANF and #DEFINE in C programming

The code below tries to parse a file containing these 3 lines: 0 2 5 9 10 12 0 1 0 2 4 1 2 3 4 2 1 4 2 3 3 -1 4 4 -3 1 2 2 6 1 and stores them in these arrays: int Line1[] = { 0, 2, 5, 9, 10, 12 }; int Line2[] = { 0, 1, 0, 2, 4, 1, 2, 3, 4, 2, 1, 4 }; double Line3[] = { 2, 3, 3, -1, 4, 4, -3, 1, 2, 2, 6, 1 }; However in p...

CSharp/DotNet: Serialization, Generics and base classes

Can someone tell me why this class requires [XmlInclude(typeof(AutoHedgerBaseDataObject))] to deserialize properly? It's not clear to me. [Serializable] [XmlInclude(typeof(AutoHedgerBaseDataObject))] public abstract class AutoHedgerCommandMessage { #region Variables private string myUpdatedBy; private string myUpdatedTime...

Nested Generics with Wildcards

Why does this work: List<?> list = new LinkedList<Integer>(); while this gives a type dismatch error: List<List<?>> list = new LinkedList<List<Integer>>(); Why is this? Ist there a way around this, without using raw types? ...

java generics/inheritance in nested hashmap

hi all, if i have two hashmaps, of types HashMap<Integer, HashMap<Integer, Police>> time_id_police; HashMap<Integer, HashMap<Integer, Ambulance>> time_id_ambulance; where Police and Ambulance both extend Rescue, how can i have a method like HashMap<Integer, HashMap<Integer, Rescue>> getRescue(){ if (a) return time_id_police; ...

StructureMap Open Generics and CacheBy Singleton

I have a number of repositories that inherit from a base class Repository. Currently I am registering in-memory implmentations with Structure map like this (and it's working great): ForRequestedType<Repository<Timeslot>>() .TheDefaultIsConcreteType<InMemoryRepository<Timeslot>>() .AsSingletons(); ForRequestedType<Repository<Ap...

java generics - method return type

hi, say i have a class Animal, and then classes Dog and Cat that extend it. Can i have a method then that returns dog or cat, depending on some value? so, something like this, except working :) public <T extends Animal> getAnimal(){ if (a) return new Dog(); else return new Cat(); } ...

Implement an Interface with Generic Methods

I'm drawing a blank on this one and can't seem to find any previous example that I wrote. I'm trying to implement a generic interface with a class. When I implement the interface I think something isn't working right because Visual Studio continually produces errors saying that I'm not implmenting all of the methods in the Generic Interf...

Explanation why IEnumerable is more efficient then a List

I keep hearing that in .net 3.5 you should use IEnumerable over a List, but I can’t find any reference materials or articles that explain why it’s so much more proficient. Does anyone know of any content that explains this? The purpose of asking this question is to get a better understanding of what IEnumerable is doing under the hood...

Is there a C# generic constraint for "real number" types?

Greets! I'm attempting to set up a Cartesian coordinate system in C#, but I don't want to restrict myself to any one numerical type for my coordinate values. Sometimes they could be integers, and other times they could be rational numbers, depending on context. This screams "generic class" to me, but I'm stumped as to how to constrict...

What's the "'1" for in class names sent from Reflection/CodeDom/CLR?

I can't remember exactly where I've seen this strange '1 (single-tick and the number 1) appearing next to classnames, but it's shown up when inspecting variable values while debugging and most recently in the answer to this question. targetClass.BaseTypes.Add(new CodeTypeReference { BaseType = "DataObjectBase`1[Refund]", Options = CodeT...

Initialize IList<T> C#

Hi SO: Is there a particular way to initialize an IList<T>? This does not seem to work: IList<ListItem> allFaqs = new IList<ListItem>(); // Error I get here: Cannot create an instance of the interface 'IList<ListItem>' ReSharper suggests to initialize it like so: IList<ListItem> allFaqs = null; But won't that cause a Null Referenc...

C# Abstract Generic Method

C#, .net 3.5 I am trying to create a base class that has a generic method. Classes that inherit from it should specify the method's type(s). The premise for this is for creating classes that manage filtering. So I have: public abstract class FilterBase { //NEED Help Declaring the Generic method for GetFilter //public abstrac...

c# Entity Framework - Generics solution

Is there a generics solution for the following code? public static int SaveReorder(IList<int> listItems) { int result = 0; int order = 1; Entity1 db = null; using (ObjectContext context = new ObjectContext()) { foreach (int id in listItems) { db = Get(c...

what is difference between unbounded wildcard type List and raw type List?

can please help in understand difference between unbounded wildcard type List and raw type List List a; List<?>; along with this can anybody help me to understand BOUNDED TYPE PARAMETER LIST List<E extends Number>; ...

How do I make a generic List which can accept two different, unrelated types?

I have to define a List and it has two types of possible values 1)String 2)some user defined Class How can I make a List that is type safe in that it only accepts these two types? I want to avoid the use of raw List. ...

Generic classes with methods that work only for some type parameters

Say that you're writing a library to display things on the screen, so you create an IDisplayable interface. This interface has one method to create a control from the object: displayable.GetControl(). You want to create your own list type that can be displayed: MyList<T>. Now this list can only be displayed if T is an IDisplayable, so y...

Is there a way to find the type of a template (generic) parameter in Java?

I'm trying to do something like this in Java: public static <T> T foo() { return (T) bar(T); } public static Object bar(Class<?> klaz) { return klaz.newInstance(); } But the code doesn't compile since I can't substitute T for a Class<?>. With a concrete class, I can call bar like: bar(ConcreteClass.class); But the same does no...