generics

What are the advantages of using generics in method signatures?

(Thanks everyone for the answers, here is my refactored example, in turn another StackOverflow question about the Single Responsibility Principle.) Coming from PHP to C#, this syntax was intimidating: container.RegisterType<Customer>("customer1"); until I realized it expresses the same thing as: container.RegisterType(typeof(Custome...

In C#, how to instantiate a passed generic type inside a method?

How can I instantiate the type T inside my InstantiateType<T> method below? I'm getting the error: 'T' is a 'type parameter' but is used like a 'variable'.: (SCROLL DOWN FOR REFACTORED ANSWER) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestGeneric33 { class Program { ...

How do I load a generic type containing nested types from dynamically loaded assemblies?

In an assembly loaded into the current AppDomain using Assembly.LoadFrom, I have the following code: [TypeConverter(typeof(EnumConverter<Shapes>))] public enum Shapes { Triangle, Square, Circle } The generic EnumConverter<T> is defined in the assembly returned by calling Assembly.GetEntryAssembly(). When I read the TypeConverte...

Why won't this generic java code compile?

In this simplified example I have a generic class, and a method that returns a Map regardless of the type parameter. Why does the compiler wipe out the types on the map when I don't specify a type on the containing class? import java.util.Map; public class MyClass<T> { public Map<String, String> getMap() { return nu...

How can I cast a list using generics in Java?

Please consider the following snippet: public interface MyInterface { public int getId(); } public class MyPojo implements MyInterface { private int id; public MyPojo(int id) { this.id = id; } public int getId() { return id; } } public ArrayList<MyInterface> getMyInterfaces() { ArrayLi...

Anonymous Generics - Where would I use this?

I recently discovered a trick using casting by example to instantiate a generic with an anonymous type. http://brendanjerwin.com/development/dotnet/c-sharp/2009/03/19/anonymous-generics.html So, its a neat trick, but when would it be used? Any ideas? ...

Generic wildcards in variable declarations in Scala

In Java I might do this: class MyClass { private List<? extends MyInterface> list; public void setList(List<MyImpl> l) { list = l; } } ...assuming that (MyImpl implements MyInterface) of course. What is the analog for this in Scala, when using a Buffer? import java.lang.reflect._ import scala.collection.mutable._ class Sca...

Scala covariance / contravariance question

Following on from this question, can someone explain the following in Scala: class Slot[+T] (var some: T) { // DOES NOT COMPILE // "COVARIANT parameter in CONTRAVARIANT position" } I understand the distinction between T+ and T in the type declaration (it compiles if I use T). But then how does one actually write a class whi...

Generics, Inheritance and the new operator

Here is something that I find myself using from time to time and I just wanted to get some feedback on the merits of the practice. Lets say that I have a base class: abstract class RealBase { protected RealBase(object arg) { Arg = arg; } public object Arg { get; private set; } public abstract void DoThatThingY...

How can I make a type safe bag of items that all implement a generic interface?

Need to have a type-safe bag of items that all implement a generic interface. The desire is to do something like: var stringItem = new IItem<string>(); var numberItem = new IItem<int>(); var items = new List<IItem<T>>(); //T of course doesn't accomplish what I want items.Add(stringItem); items.Add(numberItem); Something like: inter...

Table showing generic equivalents of C# 1 data types

Some time ago I came across a table that listed the generic equivalents for C# 1 data types. This would be a handy reference to have around. Can anyone point me to this? E.g., instead of Hashtable use ... ...

passing void to a generic class

I'm trying to create a form that will animate something while processing a particular task (passed as a delegate to the constructor). It's working fine, but the problem I'm having is that I can't instantiate a copy of my generic class if the particular method that I want to perform has a return type of void. I understand that this is by...

Separate GUIDs in Generic classes in C#

I've created the following class: [Guid("4469031d-23e0-483c-8566-ce978ccc9a6f")] class MyGenericContianer<BasicType> : SomeOtherContainer { } This causes the folowing two classes to have the same GUID: MyGenericContianer<int> x; MyGenericContianer<float> y; I need each specific type to have it's own GUID. How would you do this? ...

Generic list FindAll() vs. foreach

I'm looking through a generic list to find items based on a certain parameter. In General, what would be the best and fastest implementation? 1. Looping through each item in the list and saving each match to a new list and returning that foreach(string s in list) { if(s == "match") { newList.Add(s); } } return new...

.NET Casting Generic List

Can someone explain to me why in .NET 2.0 if I have an interface, IPackable and a class that implements that interface, OrderItem, when I have a method that takes in a List, passing in a list of List does not work? Does anyone know how I could accomplish this functionality? Thanks Josh Code: public interface IPackable { do...

C#: Specifying the return type of an abstract method from a Base Class according to a Sub Class

I have the following structure: abstract class Base { public abstract List<...> Get(); //What should be the generic type? } class SubOne : Base { public override List<SubOne> Get() { } } class SubTwo : Base { public override List<SubTwo> Get() { } } I want to create an abstract method that returns whate...

Generic list in an interface

I am trying to implement an interface class, that contains a list of objects. How can I make the list generic, so that the implementing class defines the type of list: public interface IEntity { Guid EntityID { get; set; } Guid ParentEntityID{ get; set; } Guid RoleId { get; set; } void SetFromEntity(); void Save(); ...

What do I return if the return type of a method is Void? (Not void!)

Due to the use of Generics in Java I ended up in having to implement a function having Void as return type: public Void doSomething() { //... } and the compiler demands that I return something. For now I'm just returning null, but I'm wondering if that is good coding practice... I've also tried Void.class, void, Void.TYPE, new Vo...

Unable to make static reference to generic subclass (Java)

Hi, I have the following code: class SuperClass { public static String getName() { return "super"; } } class SubClass extends SuperClass { public static String getName() { return "sub"; } } public class Dummy<T extends SuperClass> { public void print() { System.out.println("SuperClass: " + SuperClass.getName()); ...

Limit the size of List(Of T) - VB.NET

Hello I am trying to limit the size of my generic list so that after it contains a certain amount of values it wont add any more. I am trying to do this using the Capacity property of the List object but this does not seem to work. Dim slotDates As New List(Of Date) slotDates.Capacity = 7 How would people advice limi...