generics

Which .Net Framework generic collection type will allow/provide this(key) functionality?

I'm wondering which collection-type in System.Collections.Generic will support this scenario, if there is one: Public Class MyCollectionBase(Of ItemType, KeyType) Inherits System.Collection.Generic.??? Default Public Shadows ReadOnly Property Item(ByVal key as KeyType) as ItemType Get .... End G...

Best tutorial for the classes in System.Collections.Generic and System.Collections.Specialized namespaces?

I am looking for a book or article that goes into great level of detail on the generic Collections in .NET comparing performance and providing good advice on when to use the different collections. I am interested both in theoretical and real-life information. To be more concrete, have you seen any benchmarks comparing different collecti...

C#: No casting within Generics?

While I can upcast a string to an object, I cannot upcast an IList of strings to an IList of objects. How come? What to do now other that coping all items to a new IList? static void ThisWorks() { IList<object> list = new List<object>(); list.Add("I can add a string since string : object"); } static void ThisDoesNotWork() { ...

C#: How to make a Type generic method(byte/word/dword)?

Hello, I'm new to generics and all I could find in C# is List[T] - nothing else. This is the C++ code I have to translate in C# template <class type> type Read() { type t; int s = sizeof(type); if(index + s > size) throw(std::exception("error 101")); memcpy(&t, stream + index, s); index += s; return t; } Its called like that...

Are Java generics mainly a way of forcing static type on elements of a collection?

private ArrayList<String> colors = new ArrayList<String>(); Looking at the example above, it seems the main point of generics is to enforce type on a collection. So, instead of having an array of "Objects", which need to be cast to a String at the programmer's discretion, I enforce the type "String" on the collection in the ArrayList. ...

C# Generics

Hello, I have a Read generic function that does its job very well. It reads from a buffer of bytes and returns the specific type public static T Read<T>() { // An T[] would be a reference type, and a lot easier to work with. T[] t = new T[1]; // Marshal.SizeOf will fail with types of unknown size. Try and see... int s ...

C# Generics - array?

Hello, How to redo the declaration of that C++ template function in C#? template <class type> void ReadArray(type * array, unsigned short count) { int s = sizeof(type) * count; if(index + s > size) throw(std::exception("Error 102")); memcpy(array, stream + index, s); index += s; } When called,it append bytes/word/(type) in ...

What is the best explanation for the term "generic"?

I am looking for an good explanation, maybe with some examples. In my understanding, something is "generic" when it can be used for multiple purposes. But I may be wrong... ...

How do I Copy the Values of an IDictionary into an IList Object in .Net 2.0?

If I have a: Dictionary<string, int> How do I copy all the values into a: List<int> Object? The solution needs to be something compatible with the 2.0 CLR version, and C# 2.0 - and I really dont have a better idea, other than to loop through the dictionary and add the values into the List object one-by-one. But this feels very in...

What are the type parameter naming guidelines?

I noticed, as well as saw in the Essential C# 3.0 book, that paramters are usually defined as T or TEntity For example: public class Stack<T> { } or public class EntityCollection<TEntity> { } How do you decide which name to use? Thanks ...

Why is conversion between with different type parameters is NOT allowed?

I just don't get it as it would be so useful to convert one generic container into the other? Stack <IType> stack = new Stack<SomeType>(); ...

C#: How to refer to default(T) and constructors in xml comments

I have this class you see below in the answer. There I can refer to for example the Value with <see cref="Value"/>, and the class itself with <see cref="GenericEventArgs{T}"/>. How can I refer to default(T)? Is it even possible? How can I refer to the constructors? /// <summary> /// A simple <see cref="EventArgs"/> class that can c...

What is the best way to approach creating a corporate .Net Namespace framework from scratch?

We are migrating our applications to VB.Net 2008 from Classic VB and I need to create a base namespace and business layer. My method of approach is going to be to visit our top BA and identify the common areas of our (Fixed Income) company and try to form a decent inheritence model with as much of the code in generics as possible. What'...

What is the Generic version of a Hashtable?

I been learning basics of generics and it looks like it can really improve the performance of the application. But, I am not able to see the generic equivalent of Hashtable. Please share some sample C# code for creating generic hashtable classes. I need this for a demo. edit:Thanks for the answers. ...

Java - local class and generics, why compiler warning?

Named local classes are very rarely used, usually local classes are anonymous. Does anybody know why the code below generates a compiler warning? public class Stuff<E> { Iterator<E> foo() { class InIterator implements Iterator<E> { @Override public boolean hasNext() { return false; } @Override public E next() { return ...

C# Convert string to nullable type (int, double, etc...)

I am attempting to do some data conversion. Unfortunately, much of the data is in strings, where it should be int's or double, etc... So what I've got is something like: double? amount = Convert.ToDouble(strAmount); The problem with this approach is if strAmount is empty, if it's empty I want it to amount to be null, so when I add i...

C#: Cast a object to a unsigned number type using Generics

Hello guys, I'm trying to write some code to convert data from a object type field (come from a DataSet) into it's destination (typed) fields. I'm doing (trying at least) it using dynamic conversion. It seems to work fine for strings, int, DateTime. But it doesn't work for unsigned types (ulong, uint). Below there's a simple code that ...

Generics: Accessing New Members, Not Hidden Members

I have run into a problem with generics and new members. I wrote a generic class which operates on an object of type ObjectA. ObjectB derives from ObjectA and hides a few of ObjectA's members. When I supply the type of ObjectB as the type parameter to the generic class, I would expect that when I call any of the members hidden by Obje...

Function for manipulating container of Base/Derived objects

consider the following algorithm with arrays: class MyType; { // some stuff } class MySubType:MyType { // some stuff } void foo(MyType** arr, int len) { for (int i = 0;i<len;i++) // do something on arr[i]-> } void bar() { MySubType* arr[10]; // initialize all MySubType*'s in arr foo(&arr, 10); } Noth...

Is there a generic system delegate that takes 2 types and returns the second

I am looking for something like Action but I want it to be delegate U ReturnAction<T,U>(T param); Is there already a system delegate for this? I just don't want to reinvent the wheel. I did the same thing when I first needed a Predicate and then realized it existed. ...