generics

What rules are used for type inference when using Generics

Does anyone know where I can find a discussion on how type inference works when using Generics in Java. ...

Difference between generic argument constrained to an interface and just using the interface

What is the difference between this: void MyMethod(IMyInterface value) { //... } and this: void MyMethod<T>(T value) where T : IMyInterface { //... } ...

Map inheritance from generic class in Linq To SQL

Hi everyone, I'm trying to map my inheritance hierarchy to DB using Linq to SQL: Inheritance is like this, classes are POCO, without any LINQ to SQL attributes: public interface IStage { ... } public abstract class SimpleStage<T> : IStage where T : Process { ... } public class ConcreteStage : SimpleStage<ConcreteProcess> { ... } He...

Generic code is shared for reference type why and efficiency implications

Allegedly, the native code is shared for instantiated generic types when it is instantiated with a reference type, but not for value types. Why is that? would someone explain the in-depth details? To make more concrete: class MyGenericType{ } MyGenericType<string> and MyGenericType<Shape> will have only one code generated, wherea...

Most important things about C# generics... lesson learned

What are most important things you know about generics: hidden features, common mistakes, best and most useful practices, tips... I am starting to implement most of my library/API using generics and would like to collect most common patterns, tips, etc., found in practice. Let me formalize the question: What is the most important thing...

Nullable<T> confusion

Why is the following forbidden? Nullable<Nullable<int>> whereas struct MyNullable <T> { } MyNullable<Nullable<int>> is NOT ...

C# generics: reference type vs. value

I've faced alot of confusion in regard to difference of generic reference type vs. generic value type during my API coding. Guys, the question is what are differences in regard to constraints and functionalities (most important/easily overlooked ones)between these two beasts: class ReferenceGeneric <T> where ??? { } and str...

Why is there no generic version of HybridDictionary?

Apparently there is no generic version of a HybridDictionary. Why is this? I always considered it to be a best-practice when you needed to store items in a dictionary, but didn't know how many items were going to be in it. Maybe because the Dictionary<> has better performance and less overhead than the HybridDictionary, even with only ...

Generics question (Type parameters vs Constructors)

If you have a custom collection class that stores the weights of a single class of fruits individually in floats/double, like so: 1.1, 3.3, 6.6, 4.4, ... and you would need to specify whether it's float or double and to differentiate the fruit type, is it better to do it like this: Using an enum: FruitList<float, FruitType.Orange> ...

How do I return a Generic Type through a method in a base class?

I've been trying to create a simple base class that encapsulates some of my conventions for database access. I generally create a sproc named "products_retrieve_product" to select a product based on productID. I would like the method "Retrieve" in the base class to return the type that the derived class supplies in it's definition. I...

Using iBATIS.NET with generic custom collection interfaces and Unity.

Hi, I'm trying to use a generic custom collection interface (to support injection with Microsoft Patterns and Practices Unity) in a class O/R mapped with iBATIS.NET. Does anyone know if this is possible and if so how to do it? I have an IDataItemCollection<T> interface that I map to SqlDataItemCollection<T> which extends CollectionBas...

Is it possible to remove the "Class<T>" parameter from this Java method?

I have a Java method with the following signature: public <T> List<HtmlOptionsComposite> convertToHtmlOptionsCompositeList (List<? extends T> objects, Class<T> clazz, String getValueMethodName, String getDescriptionMethodName, String getDiscontinuedMethodName) { ... } The clazz parameter is required by the ...

How do I get the type object of a genericized Enum? eg: EnumSet.noneOf(<huh?>)

I have a generic type that is parameterized on some Enum, declared like this: public class FlagsField<T extends Enum<T>> { private EnumSet<T> _flagSet; public FlagsField() { _flagSet = EnumSet.<T>noneOf( /* what goes here? */ ); } ... } I want to initialize _flagsField in the constructor as above, but can't ...

Which generic collection to use?

I have 2 separate classes: AreaProperties FieldProperties 1 AreaProperties can map to 1 FieldProperties. Without changing the design, I want a method to return a List<> of these objects What generic collection in C# would be suitable? I understand I can send 2 Lists and the function would look like: public List<AreaProperties> Sa...

What is the appropriate way to strongly type the return of a generic function?

I'm writing a filter function to return the specific type specified out of a larger collection of supertypes (objects for example). The idea is I give you an enumerable and you return me all the strings for example. you can write it this way without generics: public static IEnumerable Filter(IEnumerable source, Type type) { List<object...

A generic list of anonymous class

In C# 3.0 you can create anonymous class with the following syntax var o = new { Id = 1, Name = "Foo" }; Is there a way to add these anonymous class to a generic list? Example: var o = new { Id = 1, Name = "Foo" }; var o1 = new { Id = 2, Name = "Bar" }; List<var> list = new List<var>(); list.Add(o); list.Add(o1); Another Example...

How to convert a sortedDictionary into Dictionary?

Dictionary<string, string> optionDictionary = new Dictionary<string, string>(); optionDictionary = ....; SortedDictionary<string, string> optionsSorted; if(sorting) { optionsSorted = new SortedDictionary<string, string>(optionDictionary ); // Convert SortedDictionary into Dictionary } return optionDictionary ; ...

Select a Dictionary<T1, T2> with LINQ

I have used the "select" keyword and extension method to return an IEnumerable<T> with LINQ, but I have a need to return a generic Dictionary<T1, T2> and can't figure it out. The example I learned this from used something in a form similar to the following: IEnumerable<T> coll = from x in y select new SomeClass{ prop1 = value1, p...

IDictionary<string, string> or NameValueCollection

I have a scenario where-in I can use either NameValueCollection or IDictionary. But I would like to know which one will be better performance-wise. -- Using NameValueCollection NameValueCollection options() { NameValueCollection nc = new NameValueCollection(); nc = ....; //populate nc here if(sorting) //sort NameVa...

Difference between Enumeration<? extends ZipEntry> and Enumeration<ZipEntry>?

Is there a difference between Enumeration<? extends ZipEntry> and Enumeration<ZipEntry>? If so, what is the difference? ...