generics

C#: GetHashCode override of object containing generic array

I have a class that contains the following two properties: public int Id { get; private set; } public T[] Values { get; private set; } I have made it IEquatable<T> and overriden the object.Equals like this: public override bool Equals(object obj) { return Equals(obj as SimpleTableRow<T>); } publi...

Workaround for MethodBase.GetCurrentMethod() on Compact Framework 3.5

I want use a Linq IQueryable Toolkit in project on .NET Compact Framework. The Linq capabilities in CF is little bit shapred - i.e.: IQueryable interface is not available. So I've found third party libraries, which implements missing functionality what I need. Now I have problem with missing method "MethodBase.GetCurrentMethod()". There...

C# SelectMany

I can flatten the results of a child collection within a collection with SelectMany: // a list of Foos, a Foo contains a List of Bars var source = new List<Foo>() { ... }; var q = source.SelectMany(foo => foo.Bar) .Select(bar => bar.barId) .ToList(); this gives me the list of all Bar Ids in the Foo List. When I attempt to g...

C# Generics function

Can someone explain this behavior in Generics? I have a generic function in C# protected virtual void LoadFieldDataEditor <T> (ref T control, string strFieldName) where T : Control { //T can be different types of controls inheriting from System.Web.UI.Control if (control is TextBox) { //This line gives an error //((TextBox)...

How do I get an item in a List<> based on an integer field?

I have a List<> in my program, filled with my custom class. I want to be able to extract an object from the list by simply specifying an integer, then returning all objects that have an integer property set to that integer. I was thinking of doing it like this: int exampleint = 5; List<MyClass> extract = new List<MyClass>(); for(int i =...

Best Practice - Removing item from generic collection in C#

I'm using C# in Visual Studio 2008 with .NET 3.5. I have a generic dictionary that maps types of events to a generic list of subscribers. A subscriber can be subscribed to more than one event. private static Dictionary<EventType, List<ISubscriber>> _subscriptions; To remove a subscriber from the subscription list, I can use either o...

Can you pass an instance of a generic type to other strongly-typed methods?

I have a situation where I have a method that takes a couple of overloads. I have a fairly common scenario where if some condition is true, I call one of the overloads, otherwise I call something else. I decided to try to be clever and refactor the common code into a single generic method that would take an object and a condition, and ...

Can you use generics methods in C# if the type is unknown until runtime?

Easiest way to explain what I mean is with a code sample. This doesn't compile, but is there any way to achieve this effect: foreach(Type someType in listOfTypes) { SomeMethod<someType>(); } Would be really convenient if that would work, but it doesn't. Is there another way to achieve the same thing as above, and why doesn't C# ...

Strange ClassCastException

This: Timerange longest = Timerange.longest(breaks); if (longest.durationInHours() >= MIN_FREE_HOURS) return true; is OK. But this: if (Timerange.longest(breaks).durationInHours() >= MIN_FREE_HOURS) return true; gives: java.lang.ClassCastException Do you know why?! For simplicity: public static final <T extends Timera...

Recursive generic types

Is it possible to define a generic type in C# that references itself? E.g. I want to define a Dictionary<> that holds its type as TValue (for a hierarchy). Dictionary<string, Dictionary<string, Dictionary<string, [...]>>> ...

How can an object not be compared to null?

I have an 'optional' parameter on a method that is a KeyValuePair. I wanted an overload that passes null to the core method for this parameter, but in the core method, when I want to check if the KeyValuePair is null, I get the following error: Operator '!=' cannot be applied to operands of type System.Collections.Generic.KeyValuePair<...

Generic function Question C#

I have a generic function and the following class hiearchy: protected virtual void LoadFieldDataEditor <T1, T2> (T1 control, T2 objData, string strFieldName) where T1 : Control where T2 : BaseDataType { //I will need to access field1. //I don't know at compile time if this would be SomeType1 or //SomeType2 but all of them inherit...

Explain the generics used in the Enum declaration

Can someone please explain what this means? Enum<T extends Enum<T>> This seems like a circular definition, and I find it highly confusing to say the least. ...

Is there any way to enforce typing on NSArray, NSMutableArray, etc.?

Can I make an NSMutableArray where all the elements are of type SomeClass? ...

Do you think generic properties would be useful in .NET?

I'm not talking about generic classes that declare properties or fields with the type of a generic parameter. I'm talking about generic properties which could be applied to both generic and non-generic classes. I'm not talking about this: public class Base<T> { public T BaseProperty { get; set; } } I'm talking about this: public...

OOGenerics Design Considerations

Hi all, Here is the situation for which I am trying to find a suitable design. I need to store profiles of numbers. A profile is just a series of numbers. They can be of either int, float or decimal type. Each profile has a ProfileDescription field based on an ennumeration. Each Profile has a collection of ProfileVersion Object...

Extension methods Dictionary<TKey,TValue>.RemoveAll? Is it possible?

I've been trying to write an extension method to mimic List.RemoveAll(Predicate). So far I've got this: public static void RemoveAll<TKey,TValue>(this Dictionary<TKey,TValue> dict, Predicate<KeyValuePair<TKey,TValue>> condition) { Dictionary<TKey,TValue> temp = new Dictionary<TKey,TValue>(); ...

Can I Create a Dictionary of Generic Types?

I'd like to create a Dictionary object, with string Keys, holding values which are of a generic type. I imagine that it would look something like this: Dictionary<string, List<T>> d = new Dictionary<string, List<T>>(); And enable me to add the following: d.Add("Numbers", new List<int>()); d.Add("Letters", new List<string>()); I kno...

Is it possible to alias/reference a delegate from a delegate?

Hi, I'm restructuring opur code to use generics. We do use (out of necessity, no changes possible here) own code to attach/detach/iterate/calls delegates. Before there were classes X, Y, Z which declared their own: public delegate void Event_x_Delegate(ref ComParam pVal, out bool result); At the moment I'm passing this delegate to the...

Abstract Inheritance and Generics (with playing cards)

So, I've been working on some playing cards in Java. (Not for any practical purpose really, I just enjoy playing cards and they're good practice) Now, right now, I'm making some Card Structures, decks, hands, piles, etc. They're all essentially the same, so I figure that I'd like to use some inheritance. The problem that I've encount...