generics

Cannot Convert from 'List<T>' to 'T[]'

Hi. I am trying to pass a list object of type List<UploadQueue> to a WCF SOAP method of the same parameter type and I am getting the error: Cannot Convert from 'System.Collections.Generic.List' to 'WebAPI.Upload.UploadQueue[]' I don't understand this because my WCF method's (below) parameter type is List<UploadQueue>: ISe...

java compiler oddity: field declared in same class, yet "not visible"

The eclipse compiler refuses to compile the following code, stating that the field s is not visible. (IBM's Aspect J compiler also refuses, stating that "s could not be resolved") Why is that? public class Test { String s; void foo(Object o) { String os = getClass().cast(o).s; } } The Java Language Specification ...

Is it possible to change the default value of a primitive data type?

I recently created a generic Matrix<T> class that acts as a wrapper around a List<List<T>> collection. As far as I can tell, this class is working perfectly. I am running into a slight problem though regarding the default values of the T's. I create an instance of Matrix<int>(3, 3), which creates a 3x3 matrix of ints, all defaulted to 0...

Tricky question about generics, inheritance and chaining

For context - read this. Problem: class Program { static void Main() { var b = new bar(); b.buzz().fizz().buzz().fizz(); //cool // ^FAIL!!!...<------------------------------------ Console.ReadLine(); } } public class foo { p...

C# Problem with Generics

What I have? I have an abstract class, QueryExecutor and derived class, SqlQueryExecutor shown below. abstract class QueryExecutor<T> { public abstract T Execute(); } class SqlQueryExecutor<T> : QueryExecutor<T> where T:ICollection { public override T Execute() { Type type = typeof(T); // Do common stuff ...

Generic Method Executed with a runtime type.

I have the following code: public class ClassExample { void DoSomthing<T>(string name, T value) { SendToDatabase(name, value); } public class ParameterType { public readonly string Name; public readonly Type DisplayType; public readonly string Value; public ParameterType(st...

Method Overloading with Types C#

Hey all! I was wondering if the following is possible. Create a class that accepts an anonymous type (string, int, decimal, customObject, etc), then have overloaded methods that do different operations based on the Type. Example class TestClass<T> { public void GetName<string>() { //do work knowing that the type is a stri...

[Java6] Same source code, Eclipse build success but Maven (javac) fails

Keep getting this error when compiling using Maven: type parameters of <X>X cannot be determined; no unique maximal instance exists for type variable X with upper bounds int,java.lang.Object Generics type interference cannot be applied to primitive types. But I thought since Java5, boxing/unboxing mechanism works seamlessly between pr...

Unsafe generic cast when deserializing a Collection

public Configuration(Node node, File file) { HashMap<String, String> conf = (HashMap<String, String>) SerializationUtils.deserialize(new FileInputStream(file)); } I understand why this gives an unsafe cast warning, but what's the best/accepted way to do this safely? Is there any good way? ...

C# Generics - Strange Interview Question

An interviewer argued me "Genrics are not completely Genrics", He provided the example (Parameters int k,int d are not generic) public static void PrintThis<T>(T a, T b, T c, int k,int d) { } He asked me if i prove still it is generics , i will be allowed to take up the next round. I did not know what he is expecting from me,and wha...

Can I have a variable number of generic parameters?

In my project I have the following three interfaces, which are implemented by classes that manage merging of a variety of business objects that have different structures. public interface IMerger<TSource, TDestination> { TDestination Merge(TSource source, TDestination destination); } public interface ITwoWayMerger<TSource1, TSource...

Conversion on Generics

Goal : My intention to design a utility that could access any numeric values( int,double,byte...) and produce the squares. What i did: delegate void t<T> (T somevalues); class program { static void Main() { Console.ReadKey(true); } } class Utility { public static void Activities<T>(T[] So...

NotSupportedException when using lambda against a DataContext and a generic function constraint of interface

I'm not entirely sure the title is properly worded, but here's the situation... I noticed today while trying to create a generic save function for Linq to Sql that when I use lambda against a data context select. It breaks within a generic function with a type constraint of another generic interface. However, it works fine with LINQ syn...

Extension method for IDictionary<t, k> : The type arguments for method cannot be inferred from the usage

In an attempt to clean up a lot of repeated code, I tried implementing the extension method below: public static void AddIfNotPresent(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) { if (!dictionary.ContainsKey(key)) { dictionary.Add(key, value); } } public static ...

How to pass Generic Type parameter to lambda expression?

I have a lambda expression which accepts, a int? (nullable integer), which returns value if value exists or DBNull.Value otherwise. Func<int?, object> getId = id => id.HasValue ? id.Value : (object)DBNull.Value; The goal here is that, I want to make that expression slightly a bit more generic so that I can pass any nullable types lik...

Java generics and simple types

Hi, I was trying to write a generic class, that could operate on any simple type (int, float, etc.). I wanted to avoid repeating the same function several times, with changing the type of parameter. But since the generic parameter is a reference, I cannot do any calculations on variables of its type, like in C++ templates. So is there a...

Getting a KeyValuePair<> directly from a Dictionary<>

I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance A a (where dict.ContainsKey(a) is true). Is it possible to get the KeyValuePair containing a directly from the Dictionary? Or do I need to create a new KeyValuePair: new KeyValuePair<A, B>(a, dict[a])? ...

Using KeyValuePair in VB.NET for a reverse lookup capable dictionary (example hint in C# needs converting)

Hi, I'm currently on a VB.NET project and wish to use a KeyValuePair to facilitate a reverse lookup. I've found a great example in C# here: http://www.dreamincode.net/forums/showtopic78080.htm, however I am having a small problem converting to VB.NET (both manually and using a translator (online carlosag)). For example the syntax I woul...

Does NUnit's Is.EqualTo not work reliably for classes derived from generic classes?

Today I ran into the following problem with NUnit. I have a class, that derives from a generic class. I started to do some serialization tests and tested for equality using the Is.EqualTo() function of NUnit. I started to suspect something is wrong, when a test that should have failed passed instead. When I used obj1.Equals(obj2) inste...

C# Interface<T> { T Func<T>(T t);} : Generic Interfaces with Parameterized Methods with Generic Return Types

I thought I'd use some (what I thought was) simple generics to enforce CRUD on some Business Classes. eg. public interface IReadable <T> { T Read<T>(string ID); } and then perhaps, I could have a NoteAdapter to do C**R**UD with the Note class eg. public class NoteAdapter : IReadable<Note> { public Note Read<Note>(string ID) ...