Most voted "generic" questions
Generics are a parametric form of polymorphism that allows instantiating classes where one or more of its attributes will have their types defined during the instantiation of this class.
Learn more…127 questions
Sort by count of
-
37
votes4
answers1166
viewsWhat are the differences between Generic Types in C# and Java?
I have studied Java for quite some time and am well acquainted with the functioning of generic types in that language: I know that only exist at compile time, that suffer type Erasure at the end of…
-
34
votes1
answer4521
viewsDifferences between <T> and <?>
What is the difference among the ratings <T> and <?> in Java? What motivation in the use of these resources and where each one is fundamental?…
-
24
votes1
answer2199
viewsWhat is generic programming?
What is generic programming? It is a technique or a programming paradigm? When should we use this concept?
-
21
votes4
answers1851
viewsInfer class type from a generic
In the following class I want to assign the attribute clazz class type inferring it from the list given in the constructor. class Foo<T> { private Class<T> clazz; Foo(List<T> list)…
-
20
votes2
answers1468
views -
19
votes1
answer739
viewsWhat is the difference between "Generics" (Java/C#) and "template" (C++)
In the question What are the differences between Generic Types in C# and Java? the difference between the Generics between Java and C#. We know that C++ does not have Generics, but uses templates…
-
18
votes2
answers994
viewsWhy use a generic return?
I was looking at the signature of the class methods Optional and I don’t understand what that means <T> in front of the return of the method empty(): public static <T> Optional<T>…
-
17
votes2
answers964
viewsWhy doesn’t polymorphism work with Generics?
When trying to compile the following code I got an error. import java.util.*; class Animal { } class Cachorro extends Animal { } public class TestePoli { public static void main(String[] args) {…
-
13
votes1
answer333
viewsWhy are arrays covariant and generic are invariant?
Arrays in Java are covariant, that is, it’s perfectly cool to do something like: Number[] meuArray = new Integer[10]; Generic types are invariant. The line below does not compile: List<Number>…
-
12
votes2
answers141
viewsDynamic declaration in attribute property
As part of a microplataforma ORM I’m developing, I’m defining a generic class that implements exclusively tight Coupling (1 record x 1 object). public class Course : MicroEntity<Course> {…
-
12
votes1
answer293
viewsWhat are the best practices with implementation of Dryioc, Fluentvalidation and Lazy?
I’m building an architecture based on the use of Lazy, DryIoc e o FluentValidation. I intend to do all validations with the object. When actually using the object I will instantiate it (Start). I…
-
11
votes3
answers663
viewsClass generic property
I have a class with two properties (Name and Value). The estate Name is a string, already the property Value I want to leave the variable type. public class Field<TValue> { public string Name…
-
10
votes1
answer112
viewsWhat is the purpose of "&" in the generic type statement?
In that code, the author creates a class as follows: public abstract class GlyphIcon<T extends Enum<T> & GlyphIcons> extends Text { /* ... */ } My question is to understand the…
-
9
votes1
answer5915
viewsGeneric Tree, how to do?
I’m trying to assemble a Generic Tree in java to assemble a Boolean expression within a gene expression algorithm, this tree would hold several types of variables, I’m not sure which is the most…
-
8
votes1
answer492
viewsHow to get the generic type entity type from the top interface?
I have the following situation: public class MinhaClasse : IMinhaClasse<Carro> { //... } public static void Main(string[] args) { var foo = new MinhaClasse(); } It is possible to get the…
-
8
votes2
answers121
viewsDeclaration of an interface with Where
I’m studying a lot Pattern design, because I think they solve a lot of things and it is very opportune to study them. I took this statement on the Macoratti website and I confess, I could not…
-
8
votes1
answer452
viewsWhat is a type class "class Minhaclasseexample<T> Where T: new(){}"?
A class class ExemploClass<T> is a list? See the example class below: public abstract class MinhaClasseExemplo<T> where T: new() { public T value {get; set;} } Which means every part of…
-
8
votes3
answers3818
viewsUpdate an item from a generic list to a specific item
How do I update a specific element of a generic list by locating by ID and passing an updated object in its place, updating the name and email? class Program { static void Main(string[] args) {…
-
8
votes2
answers828
viewsParametric polymorphism and overload in Java and C++
The following question fell in IFSP’s tender procedure: In Java and C++ programming languages, parametric polymorphism is materialized, respectively, by the functionalities and/or characteristics:…
-
7
votes2
answers5391
viewsHow to update an item from a generic list?
I am trying to change the name and email of a particular item in the list below, but I couldn’t find another way to remove the item from the list and add it again updated. There is another way to…
-
7
votes3
answers454
viewsWhy was the parameterized class attribute <T> not instantiated?
I have this class that implements the IDAO interface package DAO; import java.util.ArrayList; public class BdApp<T> implements IDAO<T> { ArrayList<T> lst = new…
-
7
votes1
answer295
viewsDifference between using Generics and "any" in Typescript?
When I know it’s better to use Generics or any in Typescript?
-
6
votes1
answer3199
viewsWhat does <T> mean in . NET?
I’m working on a project and I saw a lot of code like this public class ExemploCollection<T> { ... } And I don’t understand what this <T> means. What’s the name of it, what am I looking…
-
6
votes2
answers375
viewsDoubt with inheritance in Java method
I have the interface below public interface BaseRelatorioDTO extends Serializable { public BaseFiltroDTO getFiltro(); public List<? extends BaseRespostasDTO> getRespostas(); } And I’d like to…
-
6
votes2
answers8444
viewsPass generic list as parameter
You can pass a generic list by parameter? Ex: I have one List<Pessoa> and a List<object>. and I have my method: public void FaçaAlgo(List<T> lista) { } How can I make my method…
-
6
votes2
answers249
viewsHow to specify that the constructor type is equal to the one declared in the class?
I created an object that accepts a list of any kind, like this: class Grid<T> { private Integer count; private Integer offset; private Integer limit; private List<T> list; private T…
-
6
votes1
answer2344
viewsMake a generic vector in Java
How to create a dynamic vector in Java with generic programming? This vector must have initial size 4 and be increased in size as new elements need to be inserted. The class must have: Constructor.…
-
6
votes2
answers368
viewsHow to get the properties of a type when using Generics C#
I have the following class: public class Pessoa { public int id { get; set; } public string Nome { get; set; } public DateTime DataNascimento { get; set; } } I’ve developed a method to get her…
-
6
votes1
answer133
viewsWhy do we use "<>" in Typescript?
Why do we use <> in Typescript? Example: funcaoExemplo(x: X<any>, x: X): Observable<X<any>>
-
5
votes2
answers243
viewsCast problem in a generic method that receives an array of Enum’s (Enum[])
I intend to make a generic method that receives a enum[] and that a string representing the items of array comma-separated. public static string ToSeparatedCommaString<T>(T[] enums) where T :…
-
5
votes1
answer150
viewsWhat is the "class" restriction on a generic type?
In the line below: public class Tree<TItem> where TItem : IComparable<TItem> This line I’m creating the definition of type TItem where TItem implements the interface IComparable, i.e., I…
-
5
votes1
answer109
viewsDoubt about generic types in Typescript
I am currently studying Typeorm, and wanted to create a generic controller, as it will always be the same CRUD operations. I know I can solve with native Typeorm solutions, such as getRepository(),…
-
4
votes2
answers909
viewsConvert List<T> to T... dynamically in a method call
I’m in need of something that seems to be simple, but I’ve been doing some research and trying and I haven’t found anything that helps me. Problem I have a list of the kind List<T> I receive…
-
4
votes2
answers72
viewsGeneric, extend to an X or Y
How do I get my generic class to only receive guys who extend X or Y? Example: class MinhaClasseGenerica<T extends ClasseX ou ClasseY> Sorry if you got a little confused, I’m new yet, I can’t…
-
4
votes1
answer175
viewsWhat is the difference between the declaration forms of Generics with out and in?
What is the difference between Generics statements when using out and in? Example: public class GenericClass<T> { } public class GenericClassOut<out T> { } public class…
-
4
votes1
answer1107
viewsUnit test of implementations of a Generic class
Suppose we have the following interface: public interface ICRUDService<T> { T Create(T entity); } Abstract implementation: public abstract class CrudService<T> : ICRUDService<T> {…
-
4
votes1
answer122
viewsWhy does Arrays implement Ienumerable but not Ienumerable<T>?
I was making a class that contains an Array of the class Episode: public Episode[] EpisodesList; Then I implemented the IEnumerable<T> in my class. And as expected, I implemented the method…
-
4
votes2
answers447
viewsHow to adapt / create a method that receives an anonymous class in C#
I have a method in which one of the parameters is an object of an abstract class - Objectmapper - whose purpose is to convert an object of one class into another of another class. Below, the method…
-
4
votes1
answer1339
viewsHow to get attribute of a generic type object?
I’m having difficulties p/ retrieve an object to get a specific attribute. I want to overwrite the method Equals in my class, to compare whether the title of objects are identical. In this case, it…
-
4
votes1
answer100
viewsWhat is Monomorphization?
I was reading that posting and I came across that term monomorphization I’d like to know: What is its meaning? When this process occurs? What performance gain/loss is obtained by this process?…
-
4
votes1
answer251
viewsGeneric and Comparable in Java
I have a question I can’t get answered, if anyone can give me a plausible explanation I’d be grateful. Assuming I have two classes: public class Figura implements Comparable<Figura> { private…
-
4
votes1
answer150
viewsHow to implement an abstract method with a generic class in C#
I am creating a functional Factory where I define what is its output type and what are the required implementation methods. With this implementation, when I try to use the object obj within the…
-
4
votes2
answers113
viewsGeneric types in Java method call
I wanted to understand how this works and the name they give it in Java. Follow the code snippet: public <I, O> SimpleStepBuilder<I, O> chunk(int chunkSize) { return new…
-
3
votes1
answer1312
viewsDefinition of non-generic method?
In public partial class frmAltInfo : Form Error occurs: Extension method must be defined in a non-generic Static class I don’t understand what it can be or how to fix it. Here was the mistake:…
-
3
votes1
answer2186
viewsHow to use generic types with type parameters?
I have a dictionary that serves as a type mapper with C#. var Map = new Dictionary<Type, Type>() { {typeof(A1), typeof(A2)}, {typeof(B1), typeof(B2)}, {typeof(C1), typeof(C2)}, {typeof(D1),…
-
3
votes1
answer180
viewsGeneric type in Java
I have 3 classes, Expression, Operation, Scalar public abstract class Expression<R extends Expression> { public abstract R calcular(); } public abstract class Operation<T extends…
-
3
votes2
answers133
viewsLoop Loop C# Asp components
You’d have to do a loop loop in this case? PaperBLL paperBLL = new PaperBLL(); List<PaperEO> papers = paperBLL.SelectPapers(); HyperLink1.ImageUrl =…
-
3
votes1
answer3145
viewsHow to take duplicate values from an Arraylist
I would like to know how I can separate the double values, an example: I have a Variable FormaPagto defined as a ArrayList, In this variable I have 6 data: 0 - Money 1 - Debit 2 - Debit 3 - Money 4…
-
3
votes1
answer1336
viewsHow to deserialize JSON using Gson with generic list?
I need to deserialize JSON for a generic list, but I’m having an error that I believe is in the conversion: Method call: AtualizarJSON at = (AtualizarJSON) DeserializaConsulta(AtualizarJSON.class,…
-
3
votes1
answer379
viewsPagination with generic method
I am trying to create a generic method for paging, I tried to do as the code below, but when using Skip I need to have a orderby and I’m not sure how to do. public IQueryable<TEntity>…