|
Here is an abstraction and simplification of my issue:
I have a set of toys and a corresponding box for these toys. I want the user to be able to specify the largest type of toy that the box can hold:
public class Box<T> {}
then within the Box class...
Started by Chris on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Either the interface about, you're going to need ....
The reason you need this is that if you use a single class the generic Toy, with each specific type of Toy implementing the interface...
Or base class can define the generic list.
|
|
I have a List that is guaranteed to contain just one type object. This is created by some underlying code in a library that I cannot update. I want to create a List<ObjectType> based on the incoming List object so that my calling code is talking...
Started by Shaun F on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
For example, suppose you are calling a method in an older library the generic parameters....
You can simply cast the list:
List raw = new ArrayList(); List<String> generic = (List< for generic types, use a wildcard.
|
|
This is my set up,
class CostPeriodDto : IPeriodCalculation { public decimal? a { get; set; } public decimal? b { get; set; } public decimal? c { get; set; } public decimal? d { get; set; } } interface IPeriodCalculation { decimal? a { get; set; } decimal...
Started by Simon Wright on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Use Cast<IPeriodCalculation>() :
public class CostPeriodDto : IPeriodCalculation { public decimal? a { get; set; } public decimal? b { get; set; } public decimal? c { get... .
Try return this.costPeriodList.Cast<IPeriodCalculation>().ToList().
|
Ask your Facebook Friends
|
I have class where the relevant part looks like
class C { void Method<T>(SomeClass<T> obj) { list.Add(obj); } List<?> list = new List<?>(); }
How should I define the list so that the class compiles?
I want a list of type List<...
Started by errcw on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
list = new List<SomeClass<T>>(); }
The other option would be to use an interface:
class C { void Method<T>(T obj) where T : ISomeClass { list.Add(obj); } List<ISomeClass> list type, you could make the class itself....
|
|
I have a generic class that has one type parameter (T). I needed to store a collection of these generic objects that are of different types, so I created an interface that the generic class implements as suggested here . There is a property in the generic...
Started by thedugas on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Use a different generic type as needed class XmlUser : ISomeClassUser<string> { public.
|
|
I have a legacy class that the class itself is not a generic but one of its methods return type uses generics:
public class Thing { public Collection<String> getStuff() { ... } }
getStuff() uses generics to return a collection of strings. Therefore...
Started by Steve Kuo on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Instead of Thing<?> ( parameterized type ) the Java compiler strips out all generic arguments, even thogh (as in your case) the generic type of the method has nothing to do with the generic type.
|
|
Let's say I have two generic lists of the same type. How do I combine them into one generic list of that type?
Started by JamesBrownIsDead on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
List<SomeType> list2; var list = list1.Concat(list2).ToList();
You can simply add the items from one list to the other:
list1.AddRange(list2);
If you want to keep the lists and create a new one by creating a list with....
|
|
So I have a generic list, and an oldIndex and a newIndex value.
I want to move the item at oldIndex, to newIndex...as simply as possible.
Any suggestions?
Note The item should be end up between the items at (newIndex - 1) and newIndex before it was removed...
Started by Richard Ev on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
Var item ....
I would expect either:
// Makes sure item is at newIndex after the operation T item = list[oldIndex at newIndex - 1 T item = list[oldIndex]; list.RemoveAt(oldIndex); newIndex = (newIndex > oldIndex on this machine to check.
|
|
I have a generic List (of Foo) which contains n objects of Type Foo. One of the properties of Foo is PropertyA. PropertyA can be one of ValueA, ValueB or ValueC. Is there an easy way of splitting this into three seperate Lists, one for ValueA, one for...
Started by Simon on
, 7 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
Something along the lines of
Dim dic as New Dictionary(Of TypeOfYourValues, List(Of Foo)) For Each e As Foo In myList If Not dic.ContainsKey....
A generic algorithm implementing GroupBy shouldn't be too difficult, though.
To resort to your loop idea.
|
|
Tried to run Run Code Analysis on a project here, and got a number of warnings that said something like this:
CA1002 : Microsoft.Design : Change 'List< SomeType >' in ' SomeClass.SomeProtectedOrPublicProperty ' to use Collection, ReadOnlyCollection...
Started by Svish on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
If you had used a list, you would have because they would have to ....
In short, the generic list does not have it without any change to the public interface of the class.
To anything to reduce the chance of future breaking changes .
|