generics

Free C# eBook

Does anyone know a good (free) C# eBook for Intermediate programmers? I want something that covers generics, threads, events, delegates, etc. See also: http://stackoverflow.com/questions/391523/what-are-some-good-free-programming-books#392926 ...

Java generics and array initialization

What's the explanation for the following: public class GenericsTest { //statement 1 public ArrayList<Integer>[] lists; public GenericsTest() { //statement 2 lists = new ArrayList<Integer>[4]; } } The compiler accepts statement 1. Statement 2 is flagged by the compiler for "generic array creation"....

Is there a Many to Many Collection in Java using Generics (Domain Model, not Persistence Layer)?

I seem to be using the wrong search terms for this in Google... I have written a Generic Class for Many-To-Many Associations, but I'm guessing this has already been done. It is highly likely that it exists in an implementation much better than my own. This is my first foray into writing a generic class. For a better idea of what I'm ...

Generic enforcement

I have a generic class public MyClass<TContext, T> where TContext : DataContext that effectively acts on an instance of another public class MyOtherClass<T> : IEnumerable<T> I want to enforce that the TContext has a Table<T>. Is there a clean way to enforce this? ...

How to initialize an array through generics?

Is there a way to initialize all elements of an array to a constant value through generics? ...

ASP.NET Controls and Generics

can ASP.NET controls be used with generics? Never seen this done and want a way to differentiate some controls on a page by type, ie: DateTime vs int example: public class MyGenericTextBox<T>: TextBox { public MyGenericTextBox<T>() {... } } ...

Filtering list objects from another list

I have the following class in my C# .NET 3.5 win forms app: class Field { string objectName; string objectType; string fieldName; string fieldValue; } and a List fieldList that is a datasource for a checkedlistbox. This listbox shows all the distinct objectNames from my fieldList collection. I want to create another checkedlistbo...

How to retrieve unknown type from wcf service?

I'm currently working on a wcf service that does some lookups in a database and return the data to the client. The user has entered an id of something he wants to see in a textbox. This could be a client-id, a product-id, an order-id or whatever. The lookup method on the server tries to find the id in the client table. If it's not in the...

What generic collections in C# are IXmlSerializable?

Are any of the .NET generic collections marked as IXmlSerializable? I've tried List<T> and Collection<T> but neither work out of the box. Before I roll my own collection<T>, list<T>, or dictionary<T> class, I thought I'd check to see whether Microsoft had included something that does this already. It seems like basic functionality. EDI...

C#-Array Covariance In Generic Classes

Hi, I know that C# supports covariance in arrays like this : object[] array = new string[3]; But I'm getting an error when it tries to compile the below code class Dummy<K,T> where T:K { public void foo() { K[] arr = new T[4]; } } It says "Cannot implicitly convert type 'T[]' to 'K[]' " Why I'm getting this er...

Accessing object properties from string representations

I've got a custom object (example only code for ease of understanding) ... public class MyClass { private string name; private int increment; private Guid id; public string Name { get { return name; } set { name = value; } } public int Increment { get { return increment; } ...

Check two List<int>'s for the same numbers

I have two List's which I want to check for corresponding numbers. for example List<int> a = new List<int>(){1, 2, 3, 4, 5}; List<int> b = new List<int>() {0, 4, 8, 12}; Should give the result 4. Is there an easy way to do this without too much looping through the lists? I'm on 3.0 for the project where I need this so no Linq. ...

Convert List(of object) to List(of string)

Is there a way to convert a list(of object) to a list(of string) in c# or vb.net without iterating through all the items? (Behind the scenes iteration is fine - I just want concise code) Update: The best way is probably just to do a new select myList.Select(function(i) i.ToString()) or myList.Select(i => i.ToString()); ...

Return Inherited Generics as Base Generic

I have BaseAbstractClass(of T as WebControl) (VB Generics) which inherits WebControl. BaseAbstractClass is inherited by ConcreteWrapper1, ConcreteWrapper2, and lastly, to shake things up a bit, ConcreteWrapper4. Each of these would inherit BaseAbstractClass using a different class inherited from WebControl. What I would like to do is...

Method chaining generic list extensions

I have a List of a "complex" type - an object with a few string properties. The List itself is a property of another object and contains objects of a variety of types, as shown in this abbreviated class structure: Customer { public List<Characteristic> Characteristics; . . . } Characteristic { public string Characterist...

Why won't an item in a genereic list be removed using the Removed method?

I have this class. public class Foo { public Guid Id { get; set; } public override bool Equals(object obj) { Foo otherObj = obj as Foo; return otherObj == null && otherObj.Id == this.Id; } public override int GetHashCode() { return this.Id.GetHashCode(); } } You can see I overro...

VB.NET (Of T) Comparison Operator

In the sample code, the line with the 'error comment' gives the following the error - Operator '<' is not defined for types 'T' and 'T'. Why wouldn't VB automatically call the appropriate T operator? (i.e. If T is an integer then call the integer comparison functions.) Is it possible to make this work in an elegant fashion? This i...

Java: Specifying generics on Type AND Implementation

Most references I've seen, and my IDE's code completion all have my specifying a Generic type on both a variables type and its implementation eg. List<String> string = new ArrayList<String>(); Recently I've started skipping the generic type on the implementation eg. List<String> strings = new ArrayList(); assuming that the compiler...

C#: Should a nested class in a generic class be considered generic?

namespace GenericsTest { public class AGenericClass<T> { public class NestedNonGenericClass { } } } In the example above, should NestedNonGenericClass be considered a generic class? The reflection API says it's a generic class, and even hands me the template parameters of the containing class as the...

C# type inference of a generic method type parameter where the method has no arguments

Given the following generic interface and implementing class: public interface IRepository<T> { // U has to be of type T of a subtype of T IQueryable<U> Find<U>() where U : T; } public class PersonRepository : IRepository<Employee> { } How could I call the Find method without specififying U? var repository = new EmployeeRe...