Questions tagged [generic-method]

180 questions
4
votes
2 answers

I am getting weird errors while implementing a generic method of an interface in C#. What exactly is going wrong here?

public interface IGet { T Get(K id); } public interface IDemoRepository : IGet { } public class DemoRepository : IDemoRepository { public Dto.Message Get(string messageId) { using (var db = new…
Rahul
  • 45
  • 6
4
votes
3 answers

Upper-Bounded and Lower-Bounded Wildcards in return type of Java Generic method

I was trying to solve a problem where I am not able to understand part of the answer. Following is the class BackLister: public class BackLister { // INSERT HERE { List output = new LinkedList(); for (T t : input) …
skip
  • 12,193
  • 32
  • 113
  • 153
4
votes
3 answers

How is a variable in a lambda expression given its value

How does index in the below example obtain its value? I understand that n is automatically obtained from the source numbers, but, while the meaning is clear, I do not see how index is given its value: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0…
Bill
  • 1,407
  • 1
  • 15
  • 22
4
votes
1 answer

Why does this code compile? (java generic method)

The following code: import java.util.*; public final class JavaTest { public static > int max(List list, int begin, int end) { return 5; } public static void main(String[] args) { …
nhooyr
  • 1,104
  • 1
  • 13
  • 31
4
votes
2 answers

A generic filter method for Linq-EF queries

I have various types of EF entities, all of them have a navigation property called "Employee". When generating reports the user will have the option to filter the report according to the different employee propertoes (Cost Center, gender, etc.).…
Nean Der Thal
  • 3,189
  • 3
  • 22
  • 40
4
votes
1 answer

C# Generic Method vs Casting

I'm writing a program in C# (3.5 at the moment, but likely to be adaptable to other versions as the need arises) that uses a simple plugin architecture to control input and output. Each plugin is a DLL that is loaded when the user chooses the…
MDB
  • 75
  • 9
4
votes
3 answers

Can I use a type from reflection as a type parameter?

Can I use a type from reflection as a type parameter? E.g. I want to pick a persister based on a passed object: IPersister GetPersisterFor(IEntity entity) { return GetPersisterFor(); // <-- this cannot be compiled } IPersister…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
3
votes
2 answers

Cannot resolve method `values()` for Enum inside the Generic method

I am trying to build a generic method which gets an enum as an argument and processes some information. But unfortunately, it doesn't work as I would like. Maybe you have some suggestions to improve my code and prevent the errors. private static…
tbastue
  • 73
  • 5
3
votes
2 answers

C# how to make generic method

how can i make this method generic? How should i reflect the Stock_ID to this method?Thanks in Advance public class LocalSQL where T : new() { public static async Task> GETLAST( ) { return await…
Fethullah Kaya
  • 194
  • 4
  • 16
3
votes
2 answers

Java generics - Why "incompatible types" compilation error if a class' generic type doesn't exist in an invoked method?

Please notice the code below doesn't compile, failing on the method result assignment: String s = a.method("abc");. The compilation error: incompatible types: java.lang.Object cannot be converted to java.lang.String But, when changing A a to A a…
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
3
votes
4 answers

Is there a way to define a generic method that checks for null and then create the object?

I'd like to write a method that checks where the argument is null, and if it is, returns a new object of that type. it looks like: public static T checkNull(T obj) { if (null == obj) return someHowCreateTheObjectWithTypeT(); else return…
Visus Zhao
  • 1,144
  • 2
  • 12
  • 25
3
votes
2 answers

C# Generic DateTime.ToString() with custom format

When using: DateTime.ToString().Contains("2016") Entity Framework produces: CAST(DateValue AS nvarchar(max)) LIKE '%2016%' This uses the default date-format "mon dd yyyy hh:miAM (or PM)" I would like to user "yyyy-mm-dd hh:mi:ss (24h)" which is…
Sean Thorburn
  • 1,728
  • 17
  • 31
3
votes
2 answers

Is it possible to make HtmlHelper extension method available to only those views with specific type

I'm trying to implement my own paging for IEnumerable collections. So, I have a class called PagedList. I'm done with the class itself, I just need to write an HtmlHelper extension method that will render the actual pages. This would be the simplest…
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
3
votes
2 answers

C# Accessing generic Method without knowing specific type

I have a method that converts List to the DataTable using reflection. I want to leverage that method for creating DataSet by passing multiple lists where each list may hold different Type of object. Below is the code that gives me compile time…
Mahesh
  • 982
  • 8
  • 20
3
votes
2 answers

Strange generic method call with explicit generic type behaviour

I have a non-generic class with a generic method. Generic type on this method defines output type and can't be inferred from usage so I have to explicitly provide generic type. Sometimes this type is passed on from generic caller's method type…
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
1 2
3
11 12