|
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.
|
|
I'm trying to add another restriction on a method within a generic class. Is this possible?
Pseudocode:
public class MyBaseClass<T> where T: class { public IQueryable<T> ShowThisMethod where T: class, IMyInterface { // stuff. } }
ShowThisMethod...
Started by Alex on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
You could define another class that inherits.
So the constraints can the caller to have to specify the generic Type twice.
Is not generic , the class is (it's a non-generic method of a generic class).
|
Ask your Facebook Friends
|
Currently, generics in C# do not allow any sane way to perform arithmetic. There are awkward workarounds available, but none of them are very neat and all of them reduce performance. According to this interview , an interface with arithmetic types is ...
Started by Mike K on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Generics extend.
The compiler will only allow operations on generic type parameters allow a Generic to be restricted to a list of types?
Because then it's not generic .
Generics are not C++ templates.
|
|
Sorry if the title seems confusing, but some examples are in order.
Let's say I have some Java class with a generic type parameter:
public class GenericClass<T> { }
I can create a variable typed to store an object, with the generic parameter set...
Started by Adam Batkin on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
List<GenericClass<?>> generic GenericClass<?>> generic =....
List<GenericClass<String>> stringy = ...
GenericClass<?>> generic = stringy
which should work as expected (though is pretty ugly .
|
|
Namespace GenericsTest { public class AGenericClass<T> { public class NestedNonGenericClass { } } }
In the example above, should NestedNonGenericClass be considered a generic class?
The reflection API says it's a generic class, and even hands me...
Started by CodeMangler on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Yes, your nested class absolutely is generic, because T is bound to a type....
Then it would not be Generic....
A generic class declaration or a generic struct declaration (ยง25.2) is itself a generic class it private..
|
|
At the risk of becoming the village idiot, can someone explain to me why generics are called generics? I understand their usage and benefits, but if the definition of generic is "general" and generic collections are type safe, then why isn't this a misnomer...
Started by Jonathan S. on
, 10 posts
by 10 people.
Answer Snippets (Read the full thread at stackoverflow):
If you ask it for something you with a tautology; a generic method is called generic because, as you said, its can be used the the general sense, it doesnt have a specific....
It ONLY works with types of object.
ArrayList isn't "generic".
|
|
I have a base class which is non-generic with a derived generic class. The AbstractRepository and ILoggingManager are provided by the IoC framework.
Base Class
public abstract class EventHandlerBase : AbstractDataProvider , IEventHandler { public EventHandlerBase...
Started by Antony Scott on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Should it not be more like:
public abstract(data, loggingManager) { // Initialize... .
For a generic method like ConvertAll .
For example, List<T> more of them, e.g.
A constructor in a generic type, you don't specify the type parameters.
|
|
Class test <T> where T : class { public void Write<T>() { Console.Write(typeof(T).FullName); } }
In the above class, it is possible to pass in a string for the class ( test<string> Test = new test<string> ) and then int for the...
Started by dotnetdev on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
It is possible to define a different one for all generic parameters currently in scope..
In the class, you must have multiple generic parameters.
|
|
Let me give example:
I have some generic class/interface definition:
interface IGenericCar< T > {...}
I have another class/interface that I want to relate with class above, for example:
interface IGarrage< TCar > : where TCar: IGenericCar&...
Started by File on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Idea was to avoid back-reference in generic constraint (because they don't work), so adding extra non-generic interface could be way around....
} interface.
Is not generic and constrain against that interface
interface IGenericCar { ...
|