generics

Best way of using List<T> and exposing Collection<T>

I must implement a web service which expose a list of values (integers, custom classes etc). My working solution returns a List<T>, and according to FxCop it is better to return a Collection<T> or ReadOnlyCollection<T>. If I choose to return a ReadOnlyCollection<T>, the web service shows an error like: To be XML serializable, types ...

What causes javac to issue the "uses unchecked or unsafe operations" warning

For example: javac Foo.java Note: Foo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. ...

Search for Object in Generic List

Is it possible to search for an object by one of its property in a Generic List? Public Class Customer Private _id As Integer Private _name As String Public Property ID() As Integer Get Return _id End Get Set _id = value End Set End Property Public Property Name() As String Get ...

.NET inheritance with generic interfaces

Hallo i am currently playing around with castle projects ActiveRecord and the remoting facility. my current problem is that i need the find a way the implement the save sub from IBaseRepository in my address class and i dont know how. here is my current code, VS tells me on the line "implements IAddress" that i have to implement Sub Sa...

Is there an equivalent to String.Split that returns a generic list?

I'd like to do something like this: Dim Foo as String = "a,b,c,d,e" Dim Boo as List(of String) = Foo.Split(","c) Of course Foo.Split returns a one-dimensional array of String, not a generic List. Is there a way to do this without iterating through the array to turn it into a generic List? ...

IS it OK to use an int for the key in a KeyedCollection

Often times I need a collection of non-sequential objects with numeric identifiers. I like using the KeyedCollection for this, but I think there's a serious drawback. If you use an int for the key, you can no longer access members of the collection by their index (collection[index] is now really collection[key]). Is this a serious enough...

What is your best resource about generics and their interfaces?

I have found many pieces of documentations and recommendations about IEnumerator, IEnumerable, ICollection, IList and their generic counterparts. Sadly, I didn't find yet a tutorial or book which explains the whole hierarchy of interfaces, generic implementations of those interfaces and the best usage of each type of those. What was you...

Can't compile class calling a method in an interface with a generic list argument

Hi everyone, Just got a question about generics, why doesn't this compile when using a generic List? If its not possible, anyway around it? Much appreciate any answer. // Interface used in the ServiceAsync inteface. public interface BaseObject { public String getId(); } // Class that implements the interface public class _ModelDto...

Generic Functions in VB.NET

I'm not too familiar with generics (concept or the syntax) in general (short of using them in collections and what not), but I was wondering if the following is the best way of accomplishing what I want. Actually, I'm not entirely positive generics will solve my problem in this case. I've modelled and mapped a few dozen objects in NHibe...

How do you convert a DataTable into a generic list?

Currently, I'm using: DataTable dt = CreateDataTableInSomeWay(); List<DataRow> list = new List<DataRow>(); foreach (DataRow dr in dt.Rows) { list.Add(dr); } Is there a better/magic way? ...

Nullable type as a generic parameter possible?

I want to do something like this : myYear = record.GetValueOrNull<int?>("myYear"), Notice the nullable type as the generic paramater. Since the GetValueOrNull function could return null my first attempt was this : public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName) where T : class { object columnValu...

Accessing a property of derived class from the base class in C#

In C#, was is the best way to access a property of the derived class when the generic list contains just the base class. public class ClassA : BaseClass { public object PropertyA { get; set; } } public class ClassB: BaseClass { public object PropertyB { get; set; } } public class BaseClass { } public void Main { List<BaseC...

java Enum definition

Hi, I thought I understood Java generics pretty well, but then I came across the following in java.lang Class Enum<E extends Enum<E>> Could someone explain how to interpret this type parameter? Bonus points for providing other examples of where a similar type parameter could be used. Cheers, Don. ...

C# Generics: Can I constrain to a set of classes that don't implement an interface?

I have 3 classes that are essentially the same but don't implement an interface because they all come from different web services. e.g. Service1.Object1 Service2.Object1 Service3.Object1 They all have the same properties and I am writing some code to map them to each other using an intermediary object which implements my own interf...

Generics in legacy code

We've got a fairly large amount of code that just made the jump to Java 5. We've been using generics in those components targeted at being released in the Java 5 version, but the remaining code is, of course, full of raw types. I've set the compiler to generate an error for raw types and started manually clearing them, but at the present...

"CLR detected an invalid program." when calling Generic Methods

I write a large static method that takes a generic as a parameter argument. I call this method, and the framework throws a System.InvalidProgramException. This exception is thrown even before the first line of the method is executed. I can create a static class which takes the generic argument, and then make this a method of the static ...

C# generics question

Hi, I'm trying to complete a practice question from a book on generics but the question doesn't make sense to me. Here it goes. Create two classes with identical functionality. Use generics for the first class, and cast the second class to Object types. Create a for loop that uses class and the Object based class to determine which per...

C# Generics/Design Patterns: Should I use the template method?

Scenario: (If anyone has answered/viewed my questions recently this will be somewhat familar) I have 3 different web services which expose a set of objects that have commonality. I've written wrapper classes and conversion logic using generic methods to change between the intermediary objects and the service object. I have an interfa...

Can you use "where" to require an attribute in c#?

I want to make a generic class that accepts only serializable classes, can it be done with the where constraint? The concept I'm looking for is this: public class MyClass<T> where T : //[is serializable/has the serializable attribute] ...

Why does IEnumerable<T> inherit from IEnumerable?

This might be a old question: Why does IEnumerable<T> inherit from IEnumerable? This is how .NET do, but it brings a little trouble. Every time I write a class implements IEumerable<T>, I have to write two GetEnumerator() functions, one for IEnumerable<T> and the other for IEnumerable. And, IList<T> doesn't inherit from IList. I don'...