generics

Solution for overloaded operator constraint in .NET generics

What would I do if I want to have a generic method that only accepts types that have overloaded an operator, for instance the subtraction operator. I tried using an interface as a constraint but interfaces can't have operator overloading. What is the best way to achieve this? ...

What is the least amount of code needed to update one list with another list?

Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And another list... IList<int> newList = new List<int>(); newList.add(1); newList.add(5); newList.add(7); newList.add(11); How can I update originalList so that: If the int appears in newList, ke...

How do I execute conditional logic based upon the type passed to a VB.NET generic method

I want to create a VB.NET generic factory method that creates instances of classes (as a home-grown inversion of control container). If I pass the interface IDoSomething as the generic parameter, I want to return an instance of DoSomething (that implements IDoSomething). I cannot figure out the syntax of the if statement. I want to wr...

How can I use a type with generic arguments as a constraint?

I would like to specify a constraint which is another type with a generic argument. class KeyFrame<T> { public float Time; public T Value; } // I want any kind of Keyframe to be accepted class Timeline<T> where T : Keyframe<*> { } But this cannot be done in c# as of yet, (and I really doubt it will ever be). Is there any eleg...

When to use IEnumerable over IEnumerable<>

Due to the lack of generic variance in the .NET framework, is it more "correct" to have methods that handle the non-generic versions of the System.Collection interfaces, if the methods are being designed to handle multiple types? Ideally, once moved to .NET 3.5, the code would modified to change these methods into extension methods. ...

Generic type args which specificy the extending class?

I want to have a class which implements an interface, which specifies the specific subclass as a parameter. public abstract Task implements TaskStatus<Task> { TaskStatus<T> listener; protected complete() { // ugly, unsafe cast callback.complete((T) this); } } public interface TaskStatus<T> { public void complete(T...

C# - How do I define an inline method Func<T> as a parameter?

I've written a simple SessionItem management class to handle all those pesky null checks and insert a default value if none exists. Here is my GetItem method: public static T GetItem<T>(string key, Func<T> defaultValue) { if (HttpContext.Current.Session[key] == null) { HttpContext.Current.Session[key] = defaultValue.Inv...

How to create ArrayList (ArrayList<T>) from array (T[]) in Java

I have an array that is initialised like: Element[] array = {new Element(1),new Element(2),new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<Element> arraylist = ???; I am sure I have done this before, but the solution is sitting just at the edge of my memory. ...

Generic method call

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program { static int Count1<T>(T a) where T : IEnumerable<T> { return a.Count(); } static void Main(string[] args) { List<string> mystring = new Lis...

Using Generic lists on serviced component

Hello, I'm trying to use a generic List as a property on a ServicedComponent class... public class MyCOM : ServicedComponent { public enum MyEnumType { Value1, Value2, Value3 } public List<MyEnumType> MyList { private set; get; } public MyCOM() { MyList = new List<MyEnumType>(); } } The code co...

Convert Char[] to a list<byte> (c#)

How can I convert a Char[] (of any length) to a list < byte> ? ...

Type mismatch for Class Generics

I have the following code that won't compile and although there is a way to make it compile I want to understand why it isn't compiling. Can someone enlighten me as to specifically why I get the error message I will post at the end please? public class Test { public static void main(String args[]) { Test t = new Test(); t....

.Net: Convert Generic List of Objects to DataSet

Does anyone have code to do this? ...

Writing a generic class to handle built-in types

Not too practical maybe, but still interesting. Having some abstract question on matrix multiplication I have quickly implemented a matrix for ints, then tested my assumptions. And here I noticed that just int matrix is not good, if I occasionally want to use it with decimal or double. Of course, I could try just to cast all to double,...

Can I use Collections.EMPTY_LIST wihout an UncheckedException?

Is there a Generics Friendly way of using Collection.EMPTY_LIST in my Java Program. I know I could just declare one myself, but I'm just curious to know if there's a way in the JDK to do this. Something like users = Collections<User>.EMPTY_LIST; ...

C# Generic Class with "specialized" constructor

I have a class like the following: public class DropDownControl<T, Key, Value> : BaseControl where Key: IComparable { private IEnumerable<T> mEnumerator; private Func<T, Key> mGetKey; private Func<T, Value> mGetValue; private Func<Key, bool> mIsKeyInCollection; public DropDownControl(string name, IEnumerable<T> ...

How to turn these 3 methods into one using C# generics?

I have not used generics much and so cannot figure out if it is possible to turn the following three methods into one using generics to reduce duplication. Actually my code currently has six methods but if you can solve it for the three then the rest should just work anyway with the same solution. private object EvaluateUInt64(UInt6...

How can I control the name of generic WCF return types?

I've got a WCF Web Service method whose prototype is: [OperationContract] Response<List<Customer>> GetCustomers(); When I add the service reference to a client, Visual Studio (2005) creates a type called "ResponseOfArrayOfCustomerrleXg3IC" that is a wrapper for "Response<List<Customer>>". Is there any way I can control the wrapper na...

Typing generic values (C#)

When I try this with a generic class where this.value is T: if (this.value.GetType() == typeof(int)) { ((int)this.value)++; } else { throw new InvalidOperationException ("T must be an int to perform this operation"); } I get a compile-time error: "Cannot convert type 'T' to 'int'" What should I do to perform an in...

How can elements be added to a wildcard generic collection?

Why do I get compiler errors with this Java code? 1 public List<? extends Foo> getFoos() 2 { 3 List<? extends Foo> foos = new ArrayList<? extends Foo>(); 4 foos.add(new SubFoo()); 5 return foos; 6 } Where 'SubFoo' is a concrete class that implements Foo, and Foo is an interface. Errors I get with this code: On Line 3: "C...