generics

Is there a reasonable scenario for a stateful IComparer<T>?

I have never written a stateful IComparer<T> with a default constructor. All standard library implementations which I've checked in Reflector are stateless as well. Therefore, I'd like to assume that I can freely cache IComparer<T> like this: PriorityQueue<TPriority, TComparer> where TComparer : IComparer<TPriority>, new() { private...

Generic Class Problems!

I have been research this all day and I cannot find a solution. I understand the problem of using subclasses and how the compiler wont know what the class will actually be because of the possibility of setting setting it to something else before calling add. I have look everywhere and I still don't know how to fix this problem. I have an...

How to use the property grid in a form to edit any Type.

I have an App where I'd like to be able to edit any type (font, colour, point etc.) at run time and use any of the .Net default type editors. (e.g., font/ colour picker). Rather than re-invent the wheel, I decided to use the property grid control. If I pass an object of, say font, to the grid, it lists all the fields separately, with n...

C# Generic Partial Classes, interfaces, AND inheritance, all in the same question! (oh my!)

I've been working on a code generater for my projects and, while all has worked well. But now, I've stumbled upon a detail that I'm not sure how to resolve. This is for my DAL layer, which is intended to allow implementations for diferente dataSources. so I've got an interface: public interface IUsersDALBase { //defined methods } ...

Opaque dictionary key pattern in C#

I've run across a number of cases where a pattern for accessing items in a keyed collection (like a dictionary) is encumbered by the fact that the type of the key is not a simple type (string, int, double, etc) and isn't something that you would want to promote to an actual named class. C# 3.0 introduces the concept of anonymous types ...

What's the best way to set all values in a C# Dictionary<string,bool>?

What's the best way to set all values in a C# Dictionary? Here is what I am doing now, but I'm sure there is a better/cleaner way to do this: Dictionary<string,bool> dict = GetDictionary(); var keys = dict.Keys.ToList(); for (int i = 0; i < keys.Count; i++) { dict[keys[i]] = false; } I have tried some other ways with foreach, but...

java Generics legacy compitability problem

this code compiles fine and when executed produce "004b" how that happen why it do not produce an classcastException. public static void append(List list) { list.add("004b"); } public static void main(String[] args) { List<Integer> intList = new ArrayList<Integer>(); append(intList); System.out.println(intList.get(0)); ...

Generics erasure and legacy code

In many books is said that the reason for which Java's generic use erasure is to have compatibility with legacy code. Ok, very good but can anyone show me some simple examples where some generic code interact with old legacy code and vice-versa? ...

Mixture of types in generic list

Hello I'm trying to do the following: List<IRepository<IBusinessObject, ICriteria>> Repositories { get; } and call this by IRepository<ICustomer, ICustomerCriteria> cr = new CustomerRepository(); List.Add(CustomerRepository); where ICustomer and ICustomerCriteria descend from IBusinessObject and ICriteria respectively. However, ...

How to get the data value of an enum in a generic method?

Thanks to this question I managed to work out how to constrain my generic method to accept only enums. Now I'm trying to create a generic method so that I can bind a drop-down to any enum I choose, displaying the description in the drop-down, with the value equal to the numeric value of the enum value. public static object EnumToDataSo...

Generify classes that reference themselves.

Is there any way to generify this two classes? class Tag1{ public Tag1 Parent{get;set;} } class Tag2{ public Tag2 Parent{get;set;} } So I will have: class Tag1 : Tag{} class Tag2 : Tag{} Seems no, but possible that I missed something global. Thanks to Jon, I ended with the following solution: class Tag1 : Tag<T> { public...

.Net - When is List<T>.ForEach prefered over a standard foreach loop?

The generic list class has a .ForEach(Action<T> action) method. Now i've done some simple timings of how they both perform and it seems that the generic ForEach is the poorer performer. The (Snippet Compiler Friendly) code is below - public static class timer{ public static long foreachloop = 0; public static long Gforeachloo...

Using Java Generics with Enums

Update: Thanks for everyone who helped out - the answer to this one lay in what I wasn't noticing in my more complex code and what I didn't know about the Java5 covariant return types. Original Post: I've been playing around with something this morning. While I know that I could tackle this whole problem differently, I'm finding mysel...

assign type to variable, use variable with generic static class

I'm working in a C# web service with a generic static class that takes a type. I was wondering why this does not compile: Type type1 = typeof(MySnazzyType); Assert.AreEqual(0, ConnectionPool_Accessor<type1>._pool.Count); It gives this error: The type or namespace name 'type1' could not be found (are you missing a using directive ...

Casting to generic abstract class shows T as unresolved symbol

Let's say that I have this: public abstract class myClass<T> : Ob<T> where T : Ob<T>, new() Now in a method defined inside abstract myClass, I create an object of class myType and on a method defined inside myType, I pass the abstract class myClass calling it. So in my myType class, I have: public void myMethod(object caller) My q...

How to code this generic type C# snippet?

Simplified repeated code for each entity type is public IList<entity1> GetEntity1(.. query params ..) { IQueryable<entity1> query = context.entity1; query = from refDataType in query where refDataType.Id > 0 select refDataType; . . plus more changes to query same for each entity . return...

Should one use self-referencing generic inheritance like Customer : Entity<Customer>

Is it advisable to use self-referencing generic inheritance? public abstract class Entity<T> { public Guid Id {get; set;} public int Version {get; set;} public T Clone() { ... // clone routine ... return T; } } public class Customer : Entity<Customer> { public string CustomerName {ge...

How to reference proper non-generic overload in c# ?

What do i write instead of ??????? to select proper overload? using System; using System.Collections.Generic; namespace ConsoleApplication2 { class A {} class B : A {} class C : A {} class Program { static void Main(string[] args) { var l1 = new List<C>(); var l2 = new List<C>(); Compa...

Generic DuplicateValidationRule (Checking Business Objects for duplicates)

I'm trying to get this Generic DuplicateValidationRule working, which basically checks a Collection for duplicates (based on a generic business object type passed in). Lets take the IBankAccount Business Object for example: public interface IBankAccount : IMyBusinessObjectBase { IBank Bank { get; set; } IBankAccountType BankAcc...

Implementing IList interface

I am new to generics. I want to implement my own collection by deriving it from IList<T> interface. Can you please provide me some link to a class that implements IList<T> interface or provide me a code that at least implements Add and Remove methods? ...