generics

C# Generics and Collections

I have an two interfaces defined as follows: public interface IFoo { ... } Public interface IFooWrapper<T> where T : IFoo { T Foo {get;} } I want to be able to declare a collection of IFooWrappers but I don't want to specify the implementation of IFoo. Ideally I want to do something like the: IList<IFooWrapper<*>> myList; I can...

C# newbie List<Interface> question

If you have an Interface IFoo and a class Bar : IFoo, why can you do the following: List<IFoo> foo = new List<IFoo>(); foo.Add(new Bar()); But you cannot do: List<IFoo> foo = new List<Bar>(); ...

Why does TEnumerable<T> use pass-through methods?

TEnumerable<T>, the base class for all the Generics.Collections container classes, has a very strange declaration. It looks like this: type TEnumerable<T> = class abstract protected function DoGetEnumerator: TEnumerator<T>; virtual; abstract; public function GetEnumerator: TEnumerator<T>; end; function TEnumerable<T>.G...

Generic method overloading compilation error in VB

I have a problem with the VB.NET compiler failing to compile a class (in a separate C# assembly) which contains two overloads of a method with generic arguments. The equivalent code in C# compiles against the same assembly with no errors. Here are the two method signatures: protected void SetValue<T>(T newValue, ref T oldValue) protect...

How to check Array of strings contains a particular string?

I'm using .NET 2.0 I have a large array of string. I want to check whether a particular string is there in the array or not, I'm not sure, whether following code is optimized or I need to make it more optimized. please guide. string []test_arr= new string[]{"key1","key2","key3"}; Boolean testCondition = (new List<string>(test_arr)).Cont...

Incorporating Generics

Hello all, I am developing in C#.net and I believe I need to use generics for the problem I have however I don’t know where to start. Here are the steps I need to do: Get ID from user – DONE Query database table using ID - DONE Pull back one row - DONE Create a object type dependant on ID Populate object dependant on type Differe...

What's a good, generic algorithm for collapsing a set of potentially-overlapping ranges?

I have a method that gets a number of objects of this class class Range<T> { public T Start; public T End; } In my case T is DateTime, but lets use int for simplicity. I would like a method that collapses those ranges into ones that cover the same "area" but that do not overlap. So if I had the following ranges 1 to 5 3 to ...

Cannot return 'null' from generic methods?

Good afternoon, I have an generic method like public T GetLevelElement<T>(string name) where T : ILevelElement { [...] } Which basically performs a lookup in a db and in some cases it does not (and cannot return) a result and I would like to return null. However that's obviously not possible because of 'There is no...

Java generized class reference

If you have a method with the signature: Class<? extends List<String>> getObjectType() { return ?????; } How do you return a proper generic version of the List class? return List.class; //errors return List<String>.class; //errors return List.class<String>; //errors what is the proper syntax to handle this? ...

Is there a generic alternative to the ListDictionary class?

I was looking at some sample code and in it they used a ListDictionary object to store a small amount of data (around 5-10 objects or so, but this numerber could change over time). The only issue I have with using this class is that, unlike everything else I've been doing, it's not generic. This means, and correct me if I'm wrong here, t...

Is there a way to find an object's properties in List<T> using Contains?

Hi guys I was wandering how can I find out if an object already exists in my List. I'm adding "newPerson" (instance of Person class) in a List, but checking if newPerson contents/properties exists or not in the List. This piece works fine: List<Person> people = this.GetPeople(); if (people.Find(p => p.PersonID == newP...

Fastest way to Convert List<?> to List<ObjectType>

This is a Java question. What is the fastest way to convert a List<?> to a List<ObjectType>? I am aware that this is possible through iteration please exclude that option. Example by iteration, final List<ObjectType> targetList = new ArrayList<ObjectType>(); // API returns List<?> so I have no choice. List<?> resultList = resultSet.ge...

"As" operator for constrained generic types

Consider: TTest <T : class, constructor> = class public function CreateMyObject : T; end; function TTest<T>.CreateMyObject : T; var Obj : TObject; begin Obj := T.Create; Result := (Obj as T); end; Why isn't this possible? Compiler yields an "Operator not applicable to this type" error message for the as operator. T is constr...

C#: Combining adjacent ranges

As a follow up to the method that collapses overlapping ranges I thought I would try to create a method that combines adjacent ranges. Basically, after running the Collapse method you may end up with for example 1 to 5 and 6 to 10. I would like to combine those into one range, 1 to 10. This is what I have come up with so far, but it d...

Inheritance casting problem with Generics and XmlSerializer

How do I cast an instance of an object and actually make it that type of object? I have a class myClass1 that is the base class for myClass2 and myClass3. I want to use myClass1 for auditing, for auditing all I want is the data from myClass1. Because myClass2 and myClass3 inherit from myClass1 you can set an instance of myClass1 to an...

Why is List<Number> not a sub-type of List<Object>?

public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not type-check. I can't even cast the parameter as follows: wahey((List<Object>) new LinkedList<Number>()); From my research, I have gathered that the reason for not allowing this is type-safety. If we were allowed to do the above, ...

Casting to a Comparable, then Comparing

I'm really trying to like generics, but so far the trouble they've caused outweighs any benefits. Please, please show me I'm wrong. I understand the necessity of adding @SuppressWarnings("unchecked") when using generic-free frameworks (Spring, Hibernate). This alone really reduces generics' value, as does requiring classes be passed i...

Lambda Expression for Class Property

Can someone explain why the first of the two following examples is valid and the other is not? More specifically, how is a relationship created between T and TProperty in the first example? //Example 1 class SomeClass<T> where T : class { void SomeMethod<TProperty>( Expression<Func<T,TProperty>> expression ){ ... } } //Example 2...

C# virtual static method

Hi, Why is static virtual impossible? Is C# dependent or just don't have any sense in the OO world? I know the concept has already been underlined but I did not find a simple answer to the previous question. Thx ...

C# interface static method call with generics

Is there a simple way to implement this, and if possible without instanciating an object : interface I { static string GetClassName(); } public class Helper { static void PrintClassName<T>() where T : I { Console.WriteLine(T.GetClassName()); } } ...