generics

Is it possible to write a method that returns a class object of T?

If I have a base class such that public abstract class XMLSubscription <T extends XMLMessage> Is it possible to write a method in XMLSubscription that returns a class object of T? The only possible solution that I came up with is to have each descendant of XMLSubscription have a method like: public class XMLStatusSubscription extend...

What are Generics in C#?

I'm trying to build my first generic list and have run into some problems. I understand the declaration looks like " List<T> ", and I have using System.Collections.Generic; at the top of my page. However, Visual Studio doesn't recognize the T variable. What am I missing? ...

Java Arrays & Generics : Java Equivalent to C# IEnumerable<T>

So in C#, I can treat a string[] as an IEnumerable<string>. Is there a Java equivalent? ...

Is it a good idea to have a factory class using generics to instantiate objects?

Is it a good idea to have a factory class using generics to instantiate objects? Let's say I have a class Animal and some subclasses (Cat, Dog, etc): abstract class Animal { public abstract void MakeSound(); } class Cat : Animal { public override void MakeSound() { Console.Write("Mew mew"); } } class Dog : Ani...

Returning a default value. (C#)

I'm creating my own dictionary and I am having trouble implementing the TryGetValue function. When the key isn't found, I don't have anything to assign to the out parameter, so I leave it as is. This results in the following error: "The out parameter 'value' must be assigned to before control leaves the current method" So, basically,...

Why does the c# compiler emit Activator.CreateInstance when calling new in with a generic type with a new() constraint?

When you have code like the following: static T GenericConstruct<T>() where T : new() { return new T(); } The C# compiler insists on emitting a call to Activator.CreateInstance, which is considerably slower than a native constructor. I have the following workaround: public static class ParameterlessConstructor<T> where T :...

A simple eventbus for .net

Hello, I want to make a very simple event bus which will allow any client to subscribe to a particular type of event and when any publisher pushes an event on the bus using EventBus.PushEvent() method only the clients that subscribed to that particular event type will get the event. I am using c#.net 2.0 Any help/pointer would be grea...

Best way of checking content of Generic List

I have to work on some code that's using generic lists to store a collection of custom objects. Then it does something like the following to check if a given object's in the collection and do something if so: List<CustomObject> customObjects; //fill up the list List<CustomObject> anotherListofCustomObjects; //fill it up //... foreach...

Why can C# do this and C++/CLI cannot?

.NET Framework 3.5 comes with all the LINQ goodies, and also includes predefined generic Func and Action delegates. They are generic for up to 4 arguments. I am writing a C++/CLI project that (unfortunately) uses VS 2005 and must only rely on the standard 2.0 assembly set (so no System.Core). I tried defining my own generic delegates (i...

default value for generic type in c#

The docs for Dictionary.TryGetValue say: When this method returns, [the value argument] contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed unin I need to mimic this in my class. How do I find the default value for type...

Is it good practice to replace Class with Class<? extends Object> to avoid warnings?

In a bunch o' places in my code, I have something like this: public Class mySpecialMethod() { return MySpecialClass.class; } which causes the warning Class is a raw type. References to generic type Class should be parameterized. But, if I replace Class with Class<? extends Object> the warning goes away. Is this ...

Java: Generics, arrays, and the ClassCastException

I think there must be something subtle going on here that I don't know about. Consider the following: public class Foo<T> { private T[] a = (T[]) new Object[5]; public Foo() { // Add some elements to a } public T[] getA() { return a; } } Suppose that your main method contains the following: Foo<Double> f = new Foo...

Why aren't Java generic type parameters reified at runtime ?

My understanding is that C# and java differ with respect to generics in some ways, one of which is that generic type parameters are available at runtime in C#/.NET but not in Java. Why did the Java language designers do it this way? ...

Why do generics often use T?

Is there any reason for the use of 'T' in generics? Is it some kind of abbreviation? As far as I know, everything works. For example public G Say<G>(){ ... } or even public Hello Say<Hello>(){ ... } ...

Follow up: How do get some of the object from a list without Linq?

I have a question about this question. I posted a reply there but since it's been marked as answered, I don't think I'll get a response to my post there. I am running C# framework 2.0 and I would like to get some of the data from a list? The list is a List<>. How can I do that without looping and doing comparaison manually o...

What are some other languages that support "partial specialization"?

Partial template specialization is one of the most important concepts for generic programming in C++. For example: to implement a generic swap function: template <typename T> void swap(T &x, T &y) { const T tmp = x; y = x; x = tmp; } To specialize it for a vector to support O(1) swap: template <typename T, class Alloc> void swa...

C# delegate for two methods with different parameters

I am using the following methods: public void M1(Int32 a) { // acquire MyMutex DoSomething(a); // release MyMutex } and public void M2(String s, String t) { // acquire MyMutex DoSomethingElse(s, t); // release MyMutex } From what I have found so far it seems that it is not possible to use a single delegate for two metho...

WPF Designer has bug with parsing generic control with overrided property

I've created a generic lookless control with virtual property: public abstract class TestControlBase<TValue> : Control { public static readonly DependencyProperty ValueProperty; static TestControlBase() { ValueProperty = DependencyProperty.Register("Value", typeof(TValue), ...

Get the Integer value of an enumeration which is a generic

Here is the basic situation. Public Class MyEnumClass(of T) Public MyValue as T End Class This is vast oversimplification of the actual class, but basically I know that T is an enumeration (if it is not then there will be many other problems, and is a logical error made by the programmer) Basically I want to get the underlying int...

C#: Specifying behavior for the "default" keyword when using generics

Is it possible to specify my own default object instead of it being null? I would like to define my own default properties on certain objects. For example, if I have an object foo with properties bar and baz, instead of default returing null, I'd like it to be an instance of foo with bar set to "abc" and baz set to "def" -- is this pos...