generics

Creating an instance of a class that implements generic interface...

Ok I have a generic interface public IConfigurationValidator<T> { void Validate(); } a class that implements it: public class SMTPServerValidator : IConfigurationValidator<string> { public void Validate(string value) { if (string.IsNullOrEmpty(value)) { throw new Exceptio...

C# implement two different generic interfaces

I'm not sure what I'm wanting to do is even a good idea, but here's the problem anyway: I have MyClass which I want to implement two different types of the generic IEnumerable class, e.g. public class MyClass : IEnumerable<KeyValuePair<string, string>>, IEnumerable<KeyValuePair<MyEnum, string>> Now, the problem...

I wrote interesting Linq code but I have no idea how or why it works.

I'm working on a CRUD testing class because I got tired of duplicating the same test patterns when validating my NHibernate mappings. I've refactored the code and have reached the point where everything is working the way I envisioned it with one glaring irritation. Everything is based on strings which are used by reflection methods t...

C# Casting a List<ObjBase> as List<Obj>

Hi Why can I not cast a List<ObjBase> as List<Obj>? Why does the following not work: internal class ObjBase { } internal class Obj : ObjBase { } internal class ObjManager { internal List<Obj> returnStuff() { return getSomeStuff() as List<Obj>; } private List<ObjBase> getSomeStuff() { ...

Generics - where T is a number?

Hi All, I'm trying to figure a way to create a generic class for number types only, for doing some calculations. Is there a common interface for all number types (int, double, float...) that I'm missing??? If not, what will be the best way to create such a class? UPDATE: The main thing I'm trying to achieve is checking who is the bi...

Why is this generic method unsafe?

The following method generates a warning, but it looks safe to me. I'm sure the problem is with me: public <S extends CharSequence> S foo(S s) { return (S) new StringBuilder(s); } It looks like this will always return the argument s. Can anyone show an example that would cause this method to throw an exception? Edit: I'm not p...

Is there a cleaner way to initialise this generic array?

Yesterday I attempted to create an array of objects, belonging to a non-static inner class of a generic class. It seems that there is no nice way of doing so. First attempt: public class Wibble<T>{ public static void main(String...args){ new Wibble<String>(); } public Wibble(){ Bar...

Why does @SuppressWarnings break my code?

Got this idea from this previous question. http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation Anyway, my code is like this: public class Slice<E> { private E[] data; public Slice(Class<E> elementType, int size) { //@SuppresWarnings({"unchecked"}) data = (E[])Array.newInstance(elementType...

Implementing an abstract class based on some generic parameters in C#

I would like to do something like the below public interface IFormatter<TData, TFormat> { TFormat Format(TData data); } public abstract class BaseFormatter<TData> : IFormatter<TData, XElement> { public abstract XElement Format(TData data); } However, when I do the above I get an error about "The type or method has 2 generic p...

Performance penalty of typecasting and boxing/unboxing types in C# when storing generic values

I have a set-up similar to WPF's DependencyProperty and DependencyObject system. My properties however are generic. A BucketProperty has a static GlobalIndex (defined in BucketPropertyBase) which tracks all BucketProperties. A Bucket can have many BucketProperties of any type. A Bucket saves and gets the actual values of these BucketProp...

check for generic type

Hi all, I want to check if a generic variable is of a certain type but don't want to check the generic part. Let's say I have a variable of List<int> and another of List<double>. I just want to check if it is of type List<> if(variable is List) {} And not if (variable is List<int> || variable is List<double>) {} is this possible...

Java generics compiler error

Hi, When doing some not really fancy things with Java, I came over an error with generics that I was not able to understand why it doesn't work. The code is: package test; import java.util.*; public class TestClass { public static class A extends C{} public static class B extends C{} public static class C{} public static class ...

Do generics in Java avoid all ClassCastExceptins?

Since generics are only checked during compile time with Java 5, can they avoid ClassCastExceptions in all situations? ...

Some issues with sending List<T> as IEnumerable<T> to a method.

Possible Duplicate: Upcasting and generic lists Ok, I want to send a List<CardHolder> as an IEnumerable<ICardHolder> where CardHolder : ICardHolder. However, the compiler errors: Error 4 Argument '1': cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable' This seems strange to me...

How to specify parameter for generic list type extension method in c#

Hi, I am trying to make an extension method that will shuffle the contents of a generic list collection regardless of its type however im not sure what to put in between the <..> as the parameter. do i put object? or Type? I would like to be able to use this on any List collection i have. Thanks! public static void Shuffle(this List<??...

how to get the default value of a type if the type is only known as System.Type?

If I want a method that returns the default value of a given type and the method is generic I can return a default value like so: public static T GetDefaultValue() { return default(T); } Can I do something similar in case I have the type only as a System.Type object? public static object GetDefaultValue(Type type) { //??? } ...

C# Func with out parameter

Can I pass a method with an out parameter as a Func? public IList<Foo> FindForBar(string bar, out int count) { } // somewhere else public IList<T> Find(Func<string, int, List<T>> listFunction) { } Func needs a type so out won't compile there, and calling listFunction requires an int and won't allow an out in. Is there a way to do th...

Looking for a generic way to handle Linq reports & return results in XML

I'm designing a reporting engine for my project. The project will have several reports, each of which can be expressed as a Linq query; some will have parameters, while others won't. Now in order to meet my clients' requirements, I need to return the results of the queries in XML format, so that they can apply an XSL transform to make ...

C# generic type in a base class

I'm writing a system that has a set of protocol buffers (using protobuf-net), I want to define something like this in an abstract class they all inherit off: public byte[] GetBytes() however, the protocol buffer serealiser requires a type argument, is there some efficient way to get the type of the inheriting class? Example: public ...

What is Type<Type> called?

What is Type<Type> type; called (opposed to) Type type; You know, where you put the angle brackets around the type? I use this a lot, but don't know the name - it's bugging me. It's very hard to search for - Google ignores the <> characters. (note: this is Java) ...