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?
...
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...
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...
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...
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.
...
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...
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...
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.
...
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...
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...
How can I convert a Char[] (of any length) to a list < byte> ?
...
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....
Does anyone have code to do this?
...
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,...
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;
...
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> ...
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...
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...
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...
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...