generics

Making a Linked list using Generics

Hello. I am currently working hard at a long overdue assignment. I am supposed to make a linked list using generics. Or, I have an interface called Creature that the list is supposed to sort. But how? How can the list sort creatures when the interface cant have a Creature nextCreature pointer? I know of to make a linked list using norma...

how to implement class with collection of string/object pairs so that an object can be returned with generic method?

The values in a file are read as string and can be double, string or int or maybe even lists. An example file: DatabaseName=SomeBase Classes=11;12;13 IntValue=3 //this is required! DoubleValue=4.0 I was thinking something like this: class ConfigValues { private static SomeObject _utiObject; private static string _cfgF...

C# overloading with generics: bug or feature?

Let's have a following simplified example: void Foo<T>(IEnumerable<T> collection, params T[] items) { // ... } void Foo<C, T>(C collection, T item) where C : ICollection<T> { // ... } void Main() { Foo((IEnumerable<int>)new[] { 1 }, 2); } Compiler says: The type 'System.Collections.Generic.IEnumerable' cannot be...

Generic Property in C#

Hi, I'm not quite sure how to do that, but what I would like to do is to create a special type of property that will perform specific tasks at the get and set, and will be defined on generic type. For example, when writing this: MyProp<String> name; a pre-defined get and set will be performed on the string value. How can that b...

Namespace scoped aliases for generic types in C#

Let's have a following example: public class X { } public class Y { } public class Z { } public delegate IDictionary<Y, IList<Z>> Bar(IList<X> x, int i); public interface IFoo { // ... Bar Bar { get; } } public class Foo : IFoo { // ... public Bar Bar { get { return null; //... ...

how to make accessor for Dictionary in a way that returned Dictionary cannot be changed C# / 2.0

I thought of solution below because the collection is very very small. But what if it was big? private Dictionary<string, OfTable> _folderData = new Dictionary<string, OfTable>(); public Dictionary<string, OfTable> FolderData { get { return new Dictionary<string,OfTable>(_folderData); } } With List you can make: public class...

How to use List<T>.Find() on a simple collection that does not implement Find()?

Hi, I want to use List.Find() on a simple collection that does not implement Find(). The naive way I thought of, is to just wrap it with a list and execute .Find(), like this: ICollection myCows = GetAllCowsFromFarm(); // whatever the collection impl. is... var steak = new List<Cow>(myCows).Find(moo => moo.Name == "La Vache qui Rit"); ...

Emulating variadic templates in Scala

Suppose you want to have something like variadic templates (the ability to define n type parameters for a generic class) in Scala. For example you do not want to define Tuple2[+T1, +T2] and Tuple3[+T1, +T2, +T3] but Tuple[T*]. Are there other options than HLists that would support Tuple, Product and Function? ...

understanding syb boilerplate elimination

In the example given in http://web.archive.org/web/20080622204226/http://www.cs.vu.nl/boilerplate/ -- Increase salary by percentage increase :: Float -> Company -> Company increase k = everywhere (mkT (incS k)) -- "interesting" code for increase incS :: Float -> Salary -> Salary incS k (S s) = S (s * (1+k)) how come increase functio...

C# cast Foo<Bar> to Foo<object>

Hi, does anyone know if it is possible to cast a generic type with a certain type parameter (e.g. Bar) to the same generic type with the type parameter being a base type of Bar (such as object in my case). And, if it is possible, how would it be done? What I want to do is have a collection of Foo<object> but be able to add Foos with mor...

Remove duplicates from generic list<T>

Hi, I have came with solution to remove duplicates from generic list<T> in .NET 2.0 as follows: List<CaseStudy> caseStudies = CaseStudyDAO.FindCaseStudiesByDate(DateTime.Now.Date, DateTime.Now.Date.AddDays(1)); caseStudies.RemoveAll( delegate(CaseStudy c) { return caseStudies.IndexOf(c) != caseStudies.FindIn...

LINQ Generic Query with inherited base class?

I am trying to write some generic LINQ queries for my entities, but am having issue doing the more complex things. Right now I am using an EntityDao class that has all my generics and each of my object class Daos (such as Accomplishments Dao) inherit it, am example: using LCFVB.ObjectsNS; using LCFVB.EntityNS; namespace Accomplishment...

Common type for generic classes of different types

I have (for example) Dictionary of different generic types (d1, d2, d3, d4) and I want to store them in something var d1 = new Dictionary<int, string>(); var d2 = new Dictionary<int, long>(); var d3 = new Dictionary<DateTime, bool>(); var d4 = new Dictionary<string, object>(); var something = ?...

REG GENERIC METHOD

Hi buddies, I had a thought on using the generic method in c# as like we do in c++. Normally a method looks like this: public static (void/int/string) methodname((datatype) partameter) { return ...; } I had a thought whether can we implement the generics to this method like this: public static <T> methodname(<T> parta...

Marshal generic return types for com interop

Is it possible to Marshal a generic return type as non-generic for COM interop? Let's say I have the following class: [ComVisible(true)] public class Foo { public IEnumerable<string> GetStr() // Generic return type { yield break; } } I know that IEnumerable<string> implements IEnumerable. Can I force tlbexp.exe (...

Type mismatch: cannot convert from ArrayList<Data> to MyCollection

I've read similar questions here but I'm still a little confused. MyCollection extends ArrayList<MyClass> MyClass implements Data yet this gives me the "cannot convert from ArrayList to MyCollection" MyCollection mycollection = somehandler.getCollection(); where getCollection looks like this public ArrayList<Data> getCollecti...

Using generics to make an algorithm work on lists of "something" instead of only String's

Hi, I have a small algorithm which replaces the position of characters in a String: class Program { static void Main(string[] args) { String pairSwitchedStr = pairSwitch("some short sentence"); Console.WriteLine(pairSwitchedStr); Console.ReadKey(); } private static String pairSwitch(String str)...

Read-only view of a Java list with more general type parameter

Suppose I have class Foo extends Superclass. I understand why I can't do this: List<Foo> fooList = getFooList(); List<Superclass> supList = fooList; But, it would seem reasonable for me to do that if supList were somehow "read-only". Then, everything would be consistent as everything that would come out of an objList would be a Foo,...

List of objects plus a tag

I want to store a list of objects, lets say of type Car, but with an additional 'tag' property eg a boolean True/False which does not belong on the Car class. What is the best way to accomplish this? I need to pass the result between methods. ...

Dynamic Linq help, different errors depending on object passed as parameter?

I have an entityDao that is inherbited by everyone of my objectDaos. I am using Dynamic Linq and trying to get some generic queries to work. I have the following code in my generic method in my EntityDao : public abstract class EntityDao<ImplementationType> where ImplementationType : Entity { public ImplementationType getOneByValu...