generics

C# using Generics both in the Interface and its implementor class

半世苍凉 提交于 2020-02-15 08:44:08
问题 I want to create an interface that works for all the IComparable types. For example public interface SortAlgorithm<T> where T : System.IComparable<T> { List<T> Sort(List<T> input); } And I want its implementors to be generic as well with the same specification I provide in the interface. Like the example below public class InsertionSort<T> : SortAlgorithm<T> Here is my purpose for doing this. I want all my sorting algorithms to work with all types that implements the IComparable interface.

Switching on type with a generic return type

旧巷老猫 提交于 2020-02-14 17:25:47
问题 I'm working on making EF easier to unit test by writing some helpers that will make properties for me. I have a couple of backing fields private Mock<DbSet<Workflow>> mockedWorkFlows; private Mock<DbSet<WorkflowError>> mockedWorkFlowErrors; And I want a generic function to be able to return me the correct backing field with the following function public Mock<DbSet<T>> Mocked<T>(T t) where T : class { if ( (object)t is Workflow) { return mockedWorkFlows; //cannot Workflow to T } } There are

Conditional methods of Scala generic classes with restrictions for type parameters

穿精又带淫゛_ 提交于 2020-02-13 08:42:51
问题 I believe that a generic class may make one of its methods available only assuming that its type parameters conform to some additional restrictions, something like (syntax improvised on the spot): trait Col[T] extends Traversable[T] { def sum[T<:Int] :T = (0/:this)(_+_) } I guess I could use implicit parameters as evidence... Is there a language feature for this? 回答1: In this case you can only use an implicit parameter, as the type gets determined before the method call. trait Col[T] extends

convert list<int> to list<long>

ⅰ亾dé卋堺 提交于 2020-02-13 07:17:27
问题 How to convert List<int> to List<long> in C#? 回答1: Like this: List<long> longs = ints.ConvertAll(i => (long)i); This uses C# 3.0 lambda expressions; if you're using C# 2.0 in VS 2005, you'll need to write List<long> longs = ints.ConvertAll<int, long>( delegate(int i) { return (long)i; } ); 回答2: List<int> ints = new List<int>(); List<long> longs = ints.Select(i => (long)i).ToList(); 回答3: var longs = ints.Cast<long>().ToList(); 来源: https://stackoverflow.com/questions/3295957/convert-listint-to

convert list<int> to list<long>

本小妞迷上赌 提交于 2020-02-13 07:15:21
问题 How to convert List<int> to List<long> in C#? 回答1: Like this: List<long> longs = ints.ConvertAll(i => (long)i); This uses C# 3.0 lambda expressions; if you're using C# 2.0 in VS 2005, you'll need to write List<long> longs = ints.ConvertAll<int, long>( delegate(int i) { return (long)i; } ); 回答2: List<int> ints = new List<int>(); List<long> longs = ints.Select(i => (long)i).ToList(); 回答3: var longs = ints.Cast<long>().ToList(); 来源: https://stackoverflow.com/questions/3295957/convert-listint-to

Understanding bounded generics in java. What is the point?

瘦欲@ 提交于 2020-02-13 05:27:16
问题 I am trying to understand bounded types and not quite grasping the point of them. There is an example of bounded generics on which provides this use case: public class NaturalNumber<T extends Integer> { private T n; public NaturalNumber(T n) { this.n = n; } public boolean isEven() { return n.intValue() % 2 == 0; } // ... } If you are going to restrict the classes that can be the parameterized type, why not just forget the parameterization all together and have: public class NaturalNumber {

Understanding bounded generics in java. What is the point?

混江龙づ霸主 提交于 2020-02-13 05:27:14
问题 I am trying to understand bounded types and not quite grasping the point of them. There is an example of bounded generics on which provides this use case: public class NaturalNumber<T extends Integer> { private T n; public NaturalNumber(T n) { this.n = n; } public boolean isEven() { return n.intValue() % 2 == 0; } // ... } If you are going to restrict the classes that can be the parameterized type, why not just forget the parameterization all together and have: public class NaturalNumber {

Creating generic arrays in Java

折月煮酒 提交于 2020-02-11 08:55:07
问题 public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will throw out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can create an array with a generic type? Thanks. 回答1: You can't: you must pass the class as an argument: public <K> K[] toArray(Class<K> clazz) { K[] result = (K[])Array.newInstance(clazz,this.size

Creating generic arrays in Java

半城伤御伤魂 提交于 2020-02-11 08:54:32
问题 public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will throw out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can create an array with a generic type? Thanks. 回答1: You can't: you must pass the class as an argument: public <K> K[] toArray(Class<K> clazz) { K[] result = (K[])Array.newInstance(clazz,this.size

Creating generic arrays in Java

余生长醉 提交于 2020-02-11 08:53:25
问题 public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will throw out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can create an array with a generic type? Thanks. 回答1: You can't: you must pass the class as an argument: public <K> K[] toArray(Class<K> clazz) { K[] result = (K[])Array.newInstance(clazz,this.size