generics

A way to perform conversion between open and closed delegates

I need to convert an open delegate (one in which the Target is not specified) into a closed one efficiently. I have profiled my code, and the cost of using CreateDelegate() to produce a closed delegate for an instance method is a significant fraction (>60%) of the overall run time (as it takes place for each new instance of the type). ...

How can I require a generic parameter to be an enum that implements an interface?

I'm not 100% convinced that this is a good idea, but I bumped into some code today that's currently implemented as: class MyWidget <T extends Enum<T> > { MyWidget(Map<T, Integer> valueMap) { mValueMap = valueMap; } Map<T, Integer> mValueMap; } where MyWidget then offers methods that use mValueMap to convert the passed-in En...

How to mix/fix IComparable and IComparable<T> mess.

I have a helper function, which basically calls CompareTo on two objects, but does some special corner case checking, converting, etc. Originally I wrote the function as such: public static bool BetterCompare(IComparable lhs, IComparable rhs, out retCode) { ... retCode = lhs.CompareTo(rhs); ... } But the problem is that i...

Conversion of the type of a Template class?

Hi folks, I have a class named "baseClass". From this class I inherit a class names "inheritedClass" (public class inheritedClass: baseClass) The baseClass contains a public function that returns a HashSet<baseClass>. When called from the inheritedClass, the return type is obviously still HashSet<baseClass>, but I need a HashSet<inheri...

C#: Why don't generics use the most generic type it can when we don't specify one?

For example I now created a this tiny class: public static class FileSystemInfoComparers<T> where T : FileSystemInfo { public static IEqualityComparer<T> FullName { get { return new FullNameComparer(); } } private class FullNameComparer : IEqualityComparer<T> { public bool Equals(T x, T y) { return ...

Class object of generic class (java)

is there a way in java to get an instance of something like Class<List<Object>> ? ...

Threadsafe Generic Extension method usage syntax problem

Here's my extension method for invoke on a control: public static void Invoke<T>(this T c, Action<System.Windows.Forms.Control> DoWhat) where T:System.Windows.Forms.Control { if (c.InvokeRequired) c.Invoke(o=> DoWhat(c) ); else DoWhat(c); } ds is a strongly typed dataset. This works: Action<DataGridView> a = row => row...

IsOrderedBy Extension Method

In some of my tests i need to check the order of Lists and do it something like this DateTime lastDate = new DateTime(2009, 10, 1); foreach (DueAssigmentViewModel assignment in _dueAssigments) { if (assignment.DueDate < lastDate) { Assert.Fail("Not Correctly Ordered"); } lastDate = assignment.DueDate; } What i ...

c# new() keyword

What does the new() do in the code below? public class A<T> where T : B, new() ...

Is it possible to have a generic Addition method

Possible Duplicates: How to cast an object into its type? Solution for overloaded operator constraint in .NET generics Hi All, Is it possible to have a generic Addition method? something like: public void Add<T>(T a, T b) { T c = a + b;//Error } Actually operands '+' can not be applied to Type T. Is there any way aroun...

Instantiating a generic class in Java

I know Java's generics are somewhat inferior to .Net's. I have a generic class Foo<T>, and I really need to instantiate a T in Foo using a parameter-less constructor. How can one work around Java's limitation? ...

ref type and value type in Generic.

Hi All, I was working on a demo method and found something strange(at least to me :-)) enter code here class Program { public void AnotherSwap<T>(T a, T b) { T temp; temp = a; a = b; b = temp; Console.WriteLine(a); Console.WriteLine(b); } public void swap<T>(T a, T b...

Is there a way to say "method returns this" in Java?

Is there a way to say "this method returns this" using Generics? Of course, I want to override this method in subclasses, so the declaration should work well with @Override. Here is an example: class Base { public Base copyTo (Base dest) { ... copy all fields to dest ... return this; } } class X extends Base { ...

Strongly-typed ASCX in WebForms 3.5?

I'm looking to get rid of the code-behind for a control in my WebForms 3.5 application. Again bitten by the bug of how it's done in MVC, I'd like to get a step closer to this methodology by doing: <%@ Control Language="C#" Inherits="Core.DataTemplate<Models.NewsArticle>" %> This gives me the parser error you'd expect, so I remembered...

Is the T class in generic Class<T> assignable from another class?

Edit Since there were many downvotes and people who didn't understand what I'm asking for, I'll rephrase: How do I find out at runtime what is the class that foo was generified from? public boolean doesClassImplementList(Class<?> genericClass) { // help me fill this in // this method should return true if genericClass implements Li...

Java : What is - public static<T> foo() {...} ?

I saw a java function that looked something like this- public static<T> foo() {...} I know what generics are but can someone explain the in this context? Who decides what T is equal to? Whats going on here? EDIT: Can someone please show me an example of a function like this. ...

Why is Java's Class<T> generic?

Why is Java's Class<T> generic? ...

Migrating parameters from ArrayList to List<T>

I have a large project which was originally written in C#1.1 and is now targeting C#2.0. There are several classes which have methods which take an ArrayList parameter I would like to start to move these to accept List<T> as a parameter, but this can't be done all at one time, so I need to accept both parameters. My main reason for the...

Simple LINQ query

I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string like "1-15-17-8-3" through LINQ query? thank you ...

C#: Multiple type parameters in extensions

I was trying to create an extension that could slice any array-like class (since slicing is oddly absent in the standard libraries). For example: public static M Slice<M,T>(this M source, int start, int end) where M : IList<T> { //slice code } However, the compiled does not attach this method to objects of type M (even though its ...