generics

Entity Framework Generic Repository Error

I am trying to create a very generic generics repository for my Entity Framework repository that has the basic CRUD statements and uses an Interface. I have hit a brick wall head first and been knocked over. Here is my code, written in a console application, using a Entity Framework Model, with a table named Hurl. Simply trying to pul...

Javac flag to disallow raw types?

Is there any Java compiler flag that one can pass to tell the compiler to disallow the use of raw types? That is, for any generic class, let the compiler force that the parameterized version be used, and throw a compilation error otherwise? ...

Hiding a "local" type parameter in Java

Suppose I'm using an interface with a generic type parameter interface Foo<T> { T getOne(); void useOne(T t); } The intention is that the type T is abstract: it enforces a type constraint on implementations of Foo, but the client code doesn't care exactly what T is. This is no problem in the context of a generic method: public <...

Why Not Use Generic Collections?

The advantage of using generics is that it increases the type safety - you can only put in the correct type of thing, and you get out the correct type without requiring a cast. The only reason I can think of for not using generic collections is that you need to store some arbitrary data. Am I missing something? What other reasons are the...

Generic Type in constructor

I have a Generic Type Interface and want a constructor of an object to take in the Generic Interface. Like: public Constructor(int blah, IGenericType<T> instance) {} I want the code that creates this object to specify the IGenericType (use Inversion of Control). I have not seen a way for this to happen. Any suggestions to accomplish...

.NET - Convert Generic Collection to DataTable

I am trying to convert a generic collection (List) to a DataTable. I found the following code to help me do this: // Sorry about indentation public class CollectionHelper { private CollectionHelper() { } // this is the method I have been using public static DataTable ConvertTo<T>(IList<T> list) { DataTable table = CreateTable<T>();...

Complex Generics Combinations

Imagine a generic class MySet which maintains a parent MySet instance and a child MySet instance. The idea is that the parent should be able to hold a superset of T and the child a subset. So given the following sample, consider the following problem: class MySet<T> { MySet<? extends T> child; void doStuff (Collection<? extends T...

Convert Generic Dictionary to different type

Is there a quick way to convert a Generic Dictionary from one type to another I have this IDictionary<string, string> _commands; and need to pass it to a function that takes a slightly different typed Dictionary public void Handle(IDictionary<string, Object> _commands); ...

Populate list from array

if i have an array. can i populate a generic list from that array: Foo[] fooList . . . (assume populated array) // This doesn't seem to work List<Foo> newList = new List<Foo>(fooList); ...

Why is C# List<> not thread-safe?

from this site: http://crfdesign.net/programming/top-10-differences-between-java-and-c Unfortunately, List<> is not thread-safe (C#’s ArrayList and Java’s Vector are thread-safe). C# also has a Hashtable; the generic version is: what makes List<> not thread-safe? is it implementation problem on .net framework engineer's part...

Using generics to cast different user input types

I have a windows application that allows input from pen input on tablet machine. When required a form (A) will open another form (B) to allow user input. In form (B) I have the following function to return the decoded value that the user inputs with pen. private object decodedValue; public T GetDecodedValue<T>() { return (T)decod...

Java generics for policy as alternative to passing policy in constructor

I have some Java code similar to: public class Thing { private IPolicy policy; public Thing(IPolicy policy) { this.policy = policy; } public void doSomething() { this.policy.execute(); } } My question is: is it possible to do this with generics rather than passing the policy to the con...

Creating a function that uses a generic structure?

I am attempting to create a generic function that the students in my introductory VB .NET course can use to search a single dimension array of a structure. My structure and array look like this: Private Structure Survey Dim idInteger As Integer Dim membersInteger As Integer Dim incomeInteger As Integer Dim stateStrin...

Is there a reasonable approach to "default" type parameters in C# Generics?

In C++ templates, one can specify that a certain type parameter is a default. I.e. unless explicitly specified, it will use type T. Can this be done or approximated in C#? I'm looking for something like: public class MyTemplate<T1, T2=string> {} So that an instance of the type that doesn't explicitly specify T2: MyTemplate<int> t ...

Is generics the solution to this class design?

I've got an enum with possible values: public enum Language { English = 1, French = 2, German = 3 } Now i want my class to be dynamic in the sense that it can cater for multiple values based on the enum list. So if the enum list grew i can capture all possible values. Here's how my initial design ...

process items in Generic list/collections with Reflection

I have a generic class that I am using Reflection to pull out the properties of the type of the generic and looking for an attribute. I am recursing into each property to do the same for each of their properties. My issue is when I come to some sort of collection property (property that is a collection) or ICollection property. I will...

c# - cast generic class to its base non-generic class

I have following classes: public abstract class CustomerBase { public long CustomerNumber { get; set; } public string Name { get; set; } } public abstract class CustomerWithChildern<T> : CustomerBase where T: CustomerBase { public IList<T> Childern { get; private set; } public CustomerWithChildern() { Childern = new List<T>();...

Java home grown generics examples

Can anyone point me to good examples of using generics in Java? By this I mean examples of writing a generic class oneself? Most explanations read "You can define a generic class like this. Now see the Java Collections API and forget all that - just use it and be happy." What I want is more like "You can define a generic class like t...

How exactly keyword 'params' work?

The following code sample prints: T T[] T[] While first two lines are as expected, why compiler selected param array for a regular array? public class A { public void Print<T>(T t) { Console.WriteLine("T"); } public void Print<T>(params T[] t) { Console.WriteLine("T[]"); } } class Program { ...

Specialize implementation of GenericType<A,B> for case A == B?

I have a generic class which takes two type parameters, Generic<A, B>. This class has methods with signatures that are distinct so long and A and B are distinct. However, if A == B the signatures match exactly and overload resolution cannot be performed. Is it possible to somehow specify a specialisation of the method for this case? ...