generics

C# getting details from subclasses

Hello, I'm a complete newbie to C# so excuse me if this looks weird. I have an abstract class called vefHlutir namespace Klasasafn { public abstract class vefHlutur { public abstract List<String> columnNames(); public abstract List<String> toStringList(); } } //Here is an object that inherits from this ab...

C# Casting and Generics

Hello all, I've been struggling with a piece of C# code and although I have found a solution to the problem, it is by no means ideal (see DoSomething_WorksButNotIdeal() below). What I would like to do is instead of having the if, else statement (which is potentially massive depending on what types I want to support) just have a generic...

ASP.Net static objects

I'm trying to cache some information that I've retrieved from a database. I've decided to use a static List<> member to store the information. I know from my experience with List<> in multithreaded applications that I need to protect access to it with the lock statement. Do I treat any code in my Asp.Net code the exact same way? Will the...

Using First() with a generic collection through reflection

Assume a method declared as the following: public static string GetString<T>(IEnumerable<T> collection, string theProperty) How can I, using reflection, return the value of property theProperty of the first element in the generic collection? (using Linq's First() method). Thanks ...

How are Java generics different from C++ templates? Why can't I use int as a parameter?

I am trying to create ArrayList<int> = new ArrayList<int>(); in Java but that does not work. Can someone explain why int as type parameter does not work? Using Integer class for int primitive works, but can someone explain why int is not accepted? Java version 1.6 ...

Scala generics in comparison to C#

I'm just wondering about an implementation detail of sScala generics. In C# one can declare a class as: class Foo<T1>{} class Foo<T1, T2>{} In Scala however the same thing would have to be declared as class Foo0[T1]{} class Foo1[T1, T2]{} Notice how the class name is forced to be changed for multiple generic parameters. Is there ...

How to reference specific implementations of generic interfaces in XML comments?

Hi all.. Consider this scenario, 2 interfaces, 1 generic: public IGenericAdder<T> { T Add(T, T); } public IIntAdder : IGenericAdder<Int32> { } Is there someway that I can do XML comments on the generic add method, so that intellisense will show "Adds the Int32" if I do a: IIntAdder foo; foo.Add( //Show intellisense here ...

Is it possible to reference a nested generic parameter in java?

I'm not sure what the technical term for this is, but consider an interface: public interface SomeInterface<T> { public T doSomething(); } And then a second interface: public interface SomeRelatedInterface<T, D extends SomeInterface<T>> { public T doSomethingRelated(D relative); } Is it possible to craft the second interf...

Why does a static method on a generic type require a Type parameter?

public class BinarySearchTree<T> where T : IComparable<T> { public static BinarySearchTree<char> InitializeSampleCharacterBST() { var bst = new BinarySearchTree<char>(); bst.Insert('F'); bst.Insert('B'); bst.Insert('A'); bst.Insert('D'); bst.Insert('C'); bst.Insert('G'); ...

calling Type.MakeGenericType() with null arguments

I have a generic type: MyType<T1, T2, T3> and i want to do this: typeof(MyType<,,>).MakeGenericType(new [] { null, null, string}); so i end up with: MyType<,,string> But, you can't pass null types into MakeGenericType (see: http://msdn.microsoft.com/en-us/library/system.type.makegenerictype.aspx). How do I achieve this? Thanks...

Java Generic Class - Determine Type

If I am creating a java class to be generic, such as: public class Foo<T> How can one determine internally to that class, what 'T' ended up being? public ???? Bar() { //if its type 1 // do this //if its type 2 // do this //if its type 3 // do this //if its type 4 // do this } I've poke...

Non Generic Versions of Generic classes and interfaces.

I often find myself in a situation where I create a generic interface or class and then want to use different versions of this class or interface in a non generic way. For example I may have an interface like this: interface ICanCreate<T> { T NewObject(); } Which allows a class to be a factory for that type. I then want to registe...

Difference between Class and Class<?>

What is the difference between a Class and a Class<?> declaration. Class a; Class<?> b; ...

How can I make a generic unit test for all my classes that inherit from a base class?

I am creating a base class (or base entity) since all my database tables will have a companyID field. In my unit tests, I have to make sure the companyID value is correct. If I am returning a list of objects, all the companyIDs should be the same. Is there a generic way of me writing a test that will loop through all the values, that ...

C#: Invoking ForEach on a runtime-generated List<T>

I am generating a List<T> with a runtime-determined type parameter. I'd like to invoke the ForEach method to iterate over the items in the list: //Get the type of the list elements Type elementType = GetListElementType(finfo); Type listType = Type.GetType("System.Collections.Generic.List`1[" + elementTyp...

Will duplicate lamda expressions resolve to the same generated code?

This is a simple question. If the content of two lambda expressions in the same class are exactly the same, will the compiler generate and use one backer method, or will it generate a method for each instance? Example ctl.MouseOver += (sender,e) => UpdateStatus(); ctl.MouseOut += (sender,e) => UpdateStatus(); Does this generate one o...

Calling a method on a generic base class created with Activator.CreateInstance

The following program works correctly. However, I would like to change it to not use InvokeMember. I would like to be able to cast the return value from CreateInstance into GenericBase and call the Foo method directly. In this code sample I know that the type is Derived. However, in my actual code, all I know is that the type is derived ...

Nested Generic Events

I want to have a generic event that I can fire that will take a custom eventArgs> e Here is my code so far public event resultsEventHandler<T> returnResults; public delegate void resultsEventHandler<T>(object sender, resultEventArgs<ObservableEntityCollection<T>> e); protected virtual void OnreturnResults(resultEventArgs<ObservableE...

Store generic data in a non-generic class

I have a DataGridView that I want to use to store generic data. I want to keep a typed data list in the DataGridView class so that all of the sorts, etc. can be handled internally. But I don't want to have to set the type on the DataGridView since I won't know the data type until the InitializeData method is called. public class MyDat...

How to splice two C# lists into one? Or maybe use a different collection type?

List<string> list1 = new List<string>(); list1.Add("Blah"); list1.Add("Bleh"); list1.Add("Blih"); List<string> list2 = new List<string>(); list2.Add("Ooga"); list2.Add("Booga"); list2.Add("Wooga"); Is there a method to create a third list that has {"Blah", "Bleh", "Blih", "Ooga", "Booga", "Wooga"} or, alternatively, change list1 so it...