.net-generics

SkipList<T> vs Dictionary<TKey,TValue>

I've been reading about Skip Lists lately. I have a web application that executes quite complex Sql queries against static datasets. I want to implement a caching system whereby I generate an md5 hash of the sql query and then return a cached dataset for the query if it exists in the collection. Which algorithm would be better, Dictio...

Where Are Value Types Stored In (C#) Generic Collections

It is true that generic collections perform better than non-generic collections for value types. (i.e. List vs. ArrayList). But why is that, other than the boxing-unboxing step? Where are the value type objects stored once added to the collection? In non-generic collections, they'll be boxed and stored on heap, what is different in gene...

Passed an instantiated System.Type as a Type Parameter for a Generic method

I have this code snippet that I want to simplify: switch (typeString) { case "boolean": CreateSimpleRows<bool>(ref group, value); break; case "datetime": CreateSimpleRows<DateTime>(ref group, value); break; case "double": ...

How to Cast to Generic Parameter in C#?

I'm trying to write a generic method for fetching an XElement value in a strongly-typed fashion. Here's what I have: public static class XElementExtensions { public static XElement GetElement(this XElement xElement, string elementName) { // Calls xElement.Element(elementName) and returns that xElement (with some validati...

Interface, Abstract or Inheritance? C# Design Question

Hi, I have a table which houses two entities, StaticProgram and DynamicProgram. There is one column in that table called ProgramType which determines if a program is of type "Static" or "Dynamic". Though these two entities are stored in one table (I am guessing because the primitive fields for Static and Dynamic programs are exactly the...

Are the keys in a Dictionary<TKey, TValue> immutable?

I get a Dictionary<string, string> back from an API but depending on certain rules need to modify some of the key names. Before I head down the road of copying to a new Dictionary I just wanted to confirm that the keys in a Dictionary<TKey, TValue> are immutable. ...

Use interface to convert collection of objects with extensions and lambdas

I have some objects like user, address and so on, and Im converting them to business objects using extension methods: public static UserModel ToPresentationForm(this User pUser) { return new UserModel { ... map data ... }; } Also I need to convert strongly typed collections...

How can I rewrite these two almost identical functions using c# generics?

Hello chaps I have two almost identical c# functions. Because they're so similar I thought I'd try out generics, but I'm stumped on how to do it. Any suggestions, or am I barking up the wrong tree entirely? public IList<UnitTemplate> UnitTemplates { get; set; } public IList<QualTemplate> QualTemplates { get; set; } public ...

Is there a technique to differentiate class behavior on generic types?

I'd like to do something like the following, but because T is essentially just a System.Object this won't work. I know T can be constrained by an interface, but that isn't an option. public class Vborr<T> where T : struct { public Vborr() { public T Next() { if ( typeof( T ) == typeof( Double ) ) { ...

Setting EntitySet<t> properties to default using reflection

I am trying to write generic code for detaching a linq class. What I have currently is: public void Detach() { this.PropertyChanged = null; this.PropertyChanging = null; this.Categories = default(EntitySet<Categories>); this.Jobs = default(EntitySet<Jobs>); this.Tasks= default(EntitySet<Tasks>); } This is all fine...

C# casting from list of generic types

We have the following interface that we use to define how an entity should be indexed (using lucene): public interface IIndexDefinition<T> where T : IIndexable { Document Convert(T entity); } This means we can keep our index service very simple. So to index an entity we have: IndexResult IndexEntities<TEntity>(IEnumerable<TEnt...

Downcasting a generic type in C# 3.5

Why can I only upcast a generic and not downcast it? How is it not clear to the compiler that if my constraint says where T : BaseClass and U is derived from BaseClass that (U)objectOfTypeT is valid? ...

Ambiguous call between two C# extension generic methods one where T:class and other where T:struct

Consider two extension methods: public static T MyExtension<T>(this T o) where T:class public static T MyExtension<T>(this T o) where T:struct And a class: class MyClass() { ... } Now call the extension method on a instance of the above class: var o = new MyClass(...); o.MyExtension(); //compiler error here.. o.MyExtension<MyClass...

Getting Proper Type Inference for a more complicated type in a Generic Method

First off I am working in the .NET 2.0 framework Second I have this working just want it working a little more elegantly Now to the issue on hand I have a need to effectively "mirror" a Dictionary object such that if we start with an object like this Dictionary<TKey,TValue> StartDictionary; We can Mirror it like this Dictionary<TV...

C# RealProxy: generic methods?

I'm trying to handle a call of a generic method through a RealProxy, but I cannot seem to find the information about the actual type of the generic parameter used in the intercepted method call. An excerpt of the code: public override IMessage Invoke(IMessage msg) { ... string methodName = (string)msg.Properties[...