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
-
3
votes1
answer758
viewsPass Type parameter for generic Runtime method
I have a method I use to deserialize data: public static class CustomJson<T> where T: class, new() { public static T Deserialize(string data) { try { return string.IsNullOrEmpty(data) ?…
-
3
votes1
answer1325
viewsCreate Generic type in Arraylist to persist data
I’m having trouble understanding the concept of Dao and how I could create a specific type to store my data, I’m initially using a String Arraylist and need to adapt it to an Object Product…
-
3
votes4
answers815
viewsReflection of an Object that is owned by another
In my code I use a class called GenericField<T> which is used in all attributes of another class called Aplicacao. Until everything well, however, in another part of the code I need to get via…
-
3
votes1
answer3315
viewsEnsure that generic object passed as parameter is subclass of an abstract class
First I would like to say that my doubt is not specifically about "verify that a generic object passed as parameter is subclass of an abstract class.", but I couldn’t pick a better title. If you…
-
3
votes1
answer838
viewsInterface defining the signature in a generic way of the methods to be implemented
I own three classes: Repositoriosolicitant Repositoriomotorist Repositorioviagem All have the following methods with the exception of Repositorioviagem which does not have "Change". The Object can…
-
3
votes1
answer151
viewsMVC Helper with List Energy
I am creating a Helper in my Asp.Net MVC 5 project, and would like to receive as a parameter a generic list, but the following code snippet does not work: @helper MeuHelper(string param1, int…
-
3
votes1
answer811
viewsIs it possible to return the child class through a method in the parent class?
I’m making a Builder and I’d like you to behave as follows: abstract class ClassePaiBuilder { public ClassePaiBuilder SetAtributo(string atributo) { // codigo return this; } public string Build() {…
-
3
votes1
answer131
viewsVariable type of return
I want to do functions with variant input type, like this: int count(vector<auto> base, auto val) { int sum= 0; for (auto it : base) { if (it == val) { sum++; } } return sum; } or this: string…
-
3
votes2
answers107
viewsHow to make this type of signature in C#?
It is possible to apply this signature type in C#? public class Teste { public void ver(Class<? extends Teste> tipo) { } } Like?
-
3
votes3
answers823
viewsMethod that returns parent class to daughter class
I have the following classes: public class Pessoa { public int PessoaId { get; set; } public string Nome { get; set; } public DateTime DataNascimento { get; set; } } public class Cliente : Pessoa {…
-
3
votes2
answers499
viewsDefine a generic variable without defining the type
I’m doing some tests on Unity so I can practice programming. I’m new and this question can be kind of stupid. I want to know if this is possible: I created the following abstract class: public…
-
3
votes2
answers114
viewsMethods with more than one Generic
It is possible to create a method using more than one type of Generic, sort of like this. public static TResult ToEntityForType<TResult>(this DataRow row, TType type) where TType : Type {…
-
3
votes1
answer565
viewsInjection of dependency Generics<T>
Good morning friends. I hit a rough patch and I’m not getting past it. I have 2 projects, a webapi and a standard class, both in dot net core. In my standard design, I am working with Repository to…
mongodb generic dependency-injection .net-core asp.net-coreasked 7 years, 3 months ago Guilherme Dietrich 31 -
3
votes1
answer48
viewsUnable to initialize a generic object with inheritance
I’m having a hard time accepting an inherited class as a generic class type. public class Teste { private void Testando() { var dog = new Cachorro(); dog.Nome = "Toy"; dog.Patas = 4;…
-
3
votes1
answer72
viewsHow to force the user to pass a type to a generic class?
I have a generic class called Quantity<UnitType>, and in some methods of another class, I need to get an object of that class in a specific type. See the code below: interface UnitType {} enum…
-
2
votes2
answers2470
viewsMethod with generic return
How do I make a method that returns a type informed in the parameters? public <E> E to(E e) { return (E)obj; } String st = to(String); Integer it = to(Integer); But it creates error, there’s…
-
2
votes3
answers220
viewsHow to build a differentiable for a Generic class?
How would it be possible to build a differentiated constructor for classes that extend a generic class? For example, for the situation below, I would like to create a Generic builder that can…
-
2
votes1
answer240
viewsGeneric Locate Method, using ADO.NET and Procedures
I am looking for a solution from a generic repository to a DAL with a generic Find method, so there is no redundancy in my code. Using ADO.NET and Procedures. I found something in this link: What…
-
2
votes2
answers357
viewsJava Generics: Unknown wildcards and T
I was able to verify that in Java we can use both a Unknown wildcard ("?"), and "T" (what is this called?) to create generic mechanisms (classes, methods, interfaces...). But it was not clear to me…
-
2
votes2
answers689
viewsCall for generic methods in C#
I built a generic method that suits me well, but I’m having difficulty in calling this method. public List<T> CriaLista<T>(params T[] pars) { List<T> lista = new List<T>();…
-
2
votes1
answer896
viewsIdentify the type of variable received in a generic function
I have a function in Delphi that receives a kind of generic data, I would like to know what kind of data received, like this: function MinhaFuncao<T>(): T I need to know what kind is in…
-
2
votes2
answers176
viewsWhat is the Box<T> declaration for?
I was taking a look at documentation of the Hack. I know that this language is a modification of PHP to introduce type checking. I saw that they put this example to demonstrate the use of classes, I…
-
2
votes1
answer726
viewsIt is possible to create a list of generic Object
I need to generate a generic list List<?> listaGenerica; having as parameter a string with the class path, as shown below. Class class = Class.forName("com.teste.Endereco"); That’s possible?…
-
2
votes3
answers682
viewsHow to mount a lambda expression and pass it by parameter to a Generic function?
I need to pass two parameters to a generic function that returns database data and select only a few specific columns to display in one DataGridView. public List<TEntity>…
-
2
votes1
answer38
viewsWhy in a generic-type method extension do I not need to spell out a type when calling it?
In the link below is a code snippet consisting of nothing more than a Shuffle method that is used as a Extension Method of Array. Although the method is generic type, I don’t need to pass a guy when…
-
2
votes1
answer47
viewsIf B is a subtype of A, why is a collection of B not a subtype of a collection of A?
Dice: public interface A { ... } public interface B extends A { ... } private Set<B> bs = new HashSet<>(); RecebeA qualquerCoisa = new RecebeA(bs); Why does the last line compile if the…
-
2
votes1
answer178
viewsGeneric Class Receiving a Generic List>?
In c# I can implement a generic class where 1 of the received attributes is a List<>? object something like: public class Negocio<TEnt, TDto, TDao, TList<coisas>> So you can call…
-
2
votes1
answer63
viewsHow do I tell the compiler that every Observablelist<O> I pass as an argument will contain a certain method?
I have the following research method: public class Pesquisa { private static <O> ObservableList<O> pesquisarPorNome(ObservableList<O> listaObservavel, String pesquisa) {…
-
1
votes2
answers65
viewsConsulting type passed via generic method
I have the following situation: public List<TEntidade> MeuMetodo<TEntidade>() { //My Code Here } Is there any way I can catch this guy from Tentity? The ultimate goal is to use this Type…
-
1
votes1
answer177
views -
1
votes1
answer75
viewsError adding While value in List<>
I’m trying to add the value of a result of mine while, and then carry it on my GridView. But I’m having a problem adding value to mine List<>. Does not accept adding prob1.vTotal in lstTotal.…
-
1
votes1
answer462
viewsHow to use Generic Method with Dapper?
I have 2 methods from a generic repository public TEntity ObterPorId(int id) { return Db.Set<TEntity>().Find(); } public IEnumerable<TEntity> ObterTodos() { return…
-
1
votes1
answer67
viewsMethod with Generics in inherited type
How do I use two types in the method Generics, to sum only the areas of the classes Quadrado and Retangulo. I tried a method like this: public static double soma(Forma <? extends Quadrado>…
-
1
votes1
answer112
viewsIsGenericType, GetGenericTypeDefinition e Nullable.GetUnderlyingType
In a code snippet I was suggested the use of these three members in order to verify if the properties of an object are Nullable. p.PropertyType.IsGenericType &&…
-
1
votes1
answer647
viewsCan someone explain to me what is a Gothic Moon tie?
I already know the bonds while,repeat and for,but when I was seeing the types of loops, for generic" . The text was in English.
-
1
votes1
answer252
viewsWorking with Generics
First, the task: Create a new Map class, now using the name Sortedmap. It should have the same methods as the Map class of the previous task, and accept as type Key a type K that implements the…
-
1
votes0
answers74
viewsExtends and super difference - Generics Java 7
I would like to know the difference of super e extends when declaring something Eneric in java, let’s go to the code that is easier. class Pai { } class Filho extends Pai { } class Filha extends Pai…
-
1
votes1
answer209
viewsPassing as parameter a generic type in an attribute in . NET MVC
In another question I asked here I had to modify my authentication class to return the values of the model referring to the user and the authenticated user type, for example, admins, users, clients,…
-
1
votes1
answer118
viewsGeneric vector with error
I’m concluding this generic vector activity, but I’m having trouble returning the last and first element of the list. and to remove the element. public class Vetor<T>{ T[] vetor; int…
-
1
votes1
answer118
viewsHow to create a method that takes only one string and returns a generic type? . NET MVC
How could I create a method that doesn’t need to receive a type as a parameter, just a string, but would return a type? For example: public static T Guard(string guard_name) where T : class, IAuth {…
-
1
votes3
answers73
viewsHow to create a class with type inference that can have a default type in C#
I’m creating a class where I make type inference, as in the example below: public class Foo<T> { public T Bar { get; set; } } Is there any way I can set T to a default type like string? My…
-
1
votes0
answers21
viewshow to create a generic method
Many times I have to make a method to search in a table, for example getByCPF. I would like to make a generic method that I pass the name of the column and what I seek. getByColumn(String column,…
-
1
votes1
answer83
viewsSignature of a generic function in Kotlin
I was watching the part about Generics and I’d like to know why the fun <T> addToList(vararg arg : T): ArrayList<T> { and not the fun addToList(vararg arg : T): ArrayList<T> {…
-
1
votes1
answer134
viewsA generic vector only works with pointers? Why?
Hello, I know it works: typedef struct stack TStack; struct stack { int size; int top; void **S; // <------ vai apontar para um vetor de ponteiros // tipo void*, que pode ser de qualquer tipo };…
-
1
votes2
answers44
viewsReturn the Dbquery property name using Generics
How do I get the name of the property DbQuery using Generics? I have the property: public DbQuery<Pessoa> ConsultarPessoas { get; set; } And in my repository:…
-
1
votes1
answer219
viewsTypescript error: Type 'K' cannot be used to index type’T'
I’m starting with Typescript and I stalled at the time of typing the following function: const pickProp = prop => obj => obj[prop] I’m trying this way: const pickProp = <K>(prop: K)…
-
1
votes0
answers26
viewsHow to create a service structure in vuejs using typescript and Generics?
Usually when I create a Vuejs + Typescript project I like to split the Services by creating methods that perform post requests, update, get and delete but I noticed that I was repeating a lot of…
-
1
votes0
answers87
viewsCreating Azure Cosmos DB Collections
Well I was creating a Poc with Generic Interface for handling my data, but I had difficulties to do with Webapi MVC, I did with good old MVC. But for the whole JSON issue, I think I lose a lot if I…
-
1
votes1
answer160
viewsGeneric CRUD in Entity Framework with unspecified entity
I’m trying to make a generic CRUD for my project. However as I used Databasefirst I do not see how to have a generic class of Entity that can be inherited. Because it doesn’t make any sense,…
-
1
votes2
answers427
viewsHow to create a generic class that receives data from two other classes
I have three classes, the superclass Refeição, and the child classes Ingrediente and Prato, wanted to create a generic class, which would receive data from child classes to create a crud (with…