Whats the best tutorial / place for learning generics
I have a team of junior developers and they want to have better understanding and usages of generics. Anyone recommend good tutorial or articles samples that are good places to start. ...
I have a team of junior developers and they want to have better understanding and usages of generics. Anyone recommend good tutorial or articles samples that are good places to start. ...
We are supposed to instantiate our entities through a factory since they are set up differently on the client and server. I want to make sure this is the case but cant quite get it to work. public interface IEntityFactory { TEntity Create<TEntity>() where TEntity : new(); } public abstract class Entity { protected Entity() ...
This causes a compile-time exception: public sealed class ValidatesAttribute<T> : Attribute { } [Validates<string>] public static class StringValidation { } I realize C# does not support generic attributes. However, after much Googling, I can't seem to find the reason. Does anyone know why generic types cannot derive from Attribut...
Could someone explain why this works in C#.NET 2.0: Nullable<DateTime> foo; if (true) foo = null; else foo = new DateTime(0); ...but this doesn't: Nullable<DateTime> foo; foo = true ? null : new DateTime(0); The latter form gives me an compile error "Type of conditional expression cannot be determined because there is no ...
Suppose I want to create a set of observers based on type. That is to say, when they are notified of an event, they are told the type of one of the arguments and then decides whether or not to act based on if it can operate on that type. Are there any simple ways to do this? I figured this would be fairly simple to do with generics, b...
I'm still trying to get my head around using Java's generics. I have no problems at all with using typed collections, but much of the rest of it just seems to escape me. Right now I'm trying to use the JUnit "PrivateAccessor", which requires a Class[] argument with a list of all the argument types for the private method being called. ...
How to do this in Java - passing a collection of subtype to a method requiring a collection of base type? The example below gives: The method foo(Map<String,List>) is not applicable for the arguments (Map<String,MyList>) I can implement by creating a class hierarchy for the typed collections - but is it possible otherwise? public vo...
I am trying to use a Generic Linked List to hold some WorkFlow steps in my application. Here is how I'm persisting it to my database. OrderID WorkFlowStepID ParentWorkFlowStepID 178373 1 NULL 178373 2 1 178373 3 2 I get this dataset back in a datareader...
How can I select the good method (I have in the example below show 2 differents way that doesn't work). I was using instead of a variable of type Object with a IF and IS to do the job but I am trying to avoid using Object and boxing/unboxing. So I thought that Generic could do the job but I am stuck here. Here is a small snippet of cod...
I have got a template class as follows: class MyClass<T> { T field; public void myMethod() { field = new T(); // gives compiler error } } How do I create a new instance of T in my class? ...
I have a method to return a group of objects as a generic list which I then bind to a Repeater. I want to implement paging on the repeater using the PagedDataSource class but I'm not sure that this is possible as it doesn't seem to work. Will I have to change the return type of my method or is it possible to bind the PagedDataSource to ...
I have a generic method with this (dummy) code (yes I'm aware IList has predicates, but my code is not using IList but some other collection, anyway this is irrelevant for the question...) static T FindThing<T>(IList collection, int id) where T : IThing, new() { foreach T thing in collecion { if (thing.Id == id) ...
If it harder to explain using words, let's look at an example I have a generic function like this void FunctionA<T>() where T : Form, new() { } If I have a reflected type, how do I use it with the above function? I'm looking forward to do this Type a = Type.GetType("System.Windows.Forms.Form"); FunctionA<a>(); Of cause the above me...
I have a series of Extension methods to help with null-checking on IDataRecord objects, which I'm currently implementing like this: public static int? GetNullableInt32(this IDataRecord dr, int ordinal) { int? nullInt = null; return dr.IsDBNull(ordinal) ? nullInt : dr.GetInt32(ordinal); } public static int? GetNullableInt32(this...
Hi, I have a generic function which gets a interface as a type, now in one condition I have to create a new class depending on the interface. I have been thinking about it and a way to solve it would be to use an IoC but I was hoping there would be an other way because an IoC seems a bit like an overkill. below is an attempt using the...
Assuming the following class stub: public class Foo<T> { private Class<T> type; public Foo<T> () {} } Can I store the generic type T into the field type in the constructor, without changing the constructor's signature to "public Foo<T> (Class<T> type)"? If yes, how? If no, why? "type = T" doesn't seem to work. ...
Just trying to get my head around Generics by reading this enlightening article by Juval Lowy Paraphrasing.. When you define a Generic class definition, it is compiled into IL. For value-types, as soon as you request for a specific value-type, it substitutes the T with your specific value type to obtain the IL for that specific confi...
OK so I'm looking a some code which looks roughly like this: void DoSomething(object o) { if (o is Sometype1) { //cast o to Sometype and do something to it } else if (o is Sometype2) { //cast o to Sometype2 and do something to it } ... else if (o is SometypeN) { //cast o to SometypeN and do something...
I'm having some trouble navigating Java's rule for inferring generic type parameters. Consider the following class, which has an optional list parameter: import java.util.Collections; import java.util.List; public class Person { private String name; private List<String> nicknames; public Person(String name) { this(name,Coll...
Is it possible to declare an instance of a generic without knowing the type at design-time? Example: Int i = 1; List<typeof(i)> list = new List<typeof(i)>(); where the type of i could be anything, instead of having to do: List<int> list = new List<int(); ...