generics

Generic solution to simple classes + factory

My tiny mind can't come up with an elegant solution to this problem. Suppose I have class such as this: public class Foo<T> { public RecordType Type { get; set; } public T Value { get; set; } } Where RecordType may look something like this: public enum RecordType { EmptyRecord, Boolea...

In Java type arguments, does <? extends E> mean strictly subtypes only? or would E also suffice?

In Java type arguments, does mean strictly subtypes only? or would E also suffice? ...

Best way to iterate over two lists simultaneously?

Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots; ... } And I would like to iterate through the pair of each in the simplest way outside of that class. How should I go about doing that? I thought about doing this: public class Unit<MobileSuit, Pilot> ...

Using a generic type of a subclass within it's abstract superclass?

Hi there! Within my code a have the following abstract superclass public abstract class AbstractClass<Type extends A> {...} and some child classes like public class ChildClassA extends AbstractClass<GenericTypeA> {...} public class ChildClassB extends AbstractClass<GenericTypeB> {...} I'm searching for an elegant way how I can us...

Generic method to cast one arbitrary type to another in c#

I want to do something like this: public static TResult MyCast<TSource, TResult>(TSource item) { return (TResult)item; } Without restrictions on TSource or TResult and avoiding unnecessary boxing if possible. Edit: I want to stress out, that I want a simple casting of types, not elaborate type conversion here. It would be perfectl...

JAXB marshalling fields whose type is a generic type parameter

I would expect the following test to work with Sun's JAXB RI 2.2.1.1, but instead it fails with a NullPointerException in constructing the JAXBContext: public class GenericFieldMarshallTest { public static class CustomType { } public static class CustomTypeAdapter extends XmlAdapter<String, CustomType> { @Override ...

where t : multiple classes

List<T> Foo<T>(Ilist list) where T : ?? is there any way to enforce T to be one of few classes ? eventually i want to do a switch on T.. thanks. ...

Override Property with different compatible Type

I need a base class with a property where I can derive classes with the same property but different (compatible) types. The base Class can be abstract. public class Base { public virtual object prop { get; set; } } public class StrBase : Base { public override string prop { get; set; } // compiler error } public class UseIt { ...

Is it possible to implement generic-dimensional data structures?

I'm writing some of my own data structures, such as binary and quad trees, and kD-trees. Is it possible to write them in such a way that allows for any number of dimensions? Something like: KDTree<2, string> twoDTree = new KDTree<2, string>(); twoDTree[3,4] = "X,Y = (3,4)"; KDTree<4, string> fourDTree = new KDTree<4, string>(); fourDT...

generic class inherited from NameObjectCollectionBase - make type safe 'for each' loops

my class - Class GenericNameValueCollection(Of valueT) inherits from NameObjectCollectionBase, but when I iterate a collection of this class it lets me use any object in the for each loop , I want it to check the type of object and make sure that the object is the same type as the collection, but the NameObjectCollectionBase GetEnumera...

c# Template specialization

can i : class X<T> where T : EventArgs { } class X<T> where T:Exception { } in C#? ...

Cyclic generic dependenices

Let's say I have two classes. Each class has one parameter. Parameter of first class bounded to a second class and vice versa. But there is an additional requirement. This parameter must also be parametrized by class itself. This is better to be explained by example: public class Class1<T extends Class2<Class1>> { ... } public cla...

C# List<T> internals

What exactly happens when adding an object to a collection such as List? List<Person> people = new List<Person>(); Person dan = new Person() { Name="daniel", Age=10 }; people.Add(dan); dan = new Person() { Name = "hw", Age = 44 }; When I execute the above code, the List<Person> peopledoes not get affected by final line. So I am assumi...

Built-in Generic Types

Hi, I read somewhere that EventHandler is a built-in generic type. Why is that so? Could somebody explain me ways to distinguish between generic and non-generic types? Thanks, Abhi. ================ I am reading Microsoft .NET Framework Application Development Foundation 2nd edition to prepare for MCTS. In the first chapter, there w...

Generic type parameter constraint based on other type parameter in the same definition

I have type hierarchy defined like this: interface IMyClass { } interface IBase1<T> { } interface IBase2<T> { } interface IMyDerived1 : IBase1<IMyClass> { } class Base1<T, U> : IBase1<T> where U : IBase2<T> { } class Base2<T, U> : IBase2<T> where U : IBase1<T> { } class Derived1<T, U> : Base1<T, U>, IMyDerived1 where T...

Generic Linked List

Hi, Before posting my question, I would like to tell you that I have no prior experience in .Net technologies and have recently started to learn C# (and WPF). My company is looking to move onto .Net technologies and I am the only one in my team learning it, so have noone apart from you guys to discuss or ask something. So if my question...

How to find out if (Type is Type) C#

I am working with two types, one generic and the other not. I don't have instances of objects but I want to find out if ( MyType is T ) or in other words if ( MyType inherits T) Again, I am looking for: if ( Truck is Vehicle ) not if ( MyTruckObject is Vehicle) ...

What are the advantage of using a generics-where-clause call over a non-generic call?

I've seen some examples where they transformed a call like void Add(IDrawing item); into void Add<TDrawing>(TDrawing item) where TDrawing : IDrawing; Beside tricking the intellisense into displaying the name of your class instead of the interface name when calling the function, because of inferred type usage in C#4, are there any o...

C#: Enums in Interfaces

Hey all, I've seen a couple similar threads to this question, but none of them really answer the question I want to ask. For starters, unfortunately I'm working with existing API code so sadly, while there may be a better way to do what I'm asking about, I'm locked in to doing it similarly to the way it is because backwards compatibili...

Is it possible to get class of the generic argument?

Hi. I'm kind of lame with generics, but I wonder, for the following class: static class SomeClass<T> { private T value; public SomeClass(T value) { T.class? this.value = value; } public T getValue() { return value; } } If called for example: SomeClass<String> stringer = new SomeClass<String>("Hello"); Is it possible to ge...