generics

C# Naked Type constraint

Recently I have read a term "naked type constraint" in the context of Generics.What does it mean? Where do we use it? ...

Can a Type Derive from Itself?

When enforcing a Generic constraint class GenericTest { public void Sample<T>(T someValue) where T:Racer,new() { Console.WriteLine(someValue.Car); } } The Type T should be derived from the Base Type Racer (Correct me if anything wrong). public class Racer { string name; string car; pub...

Generic constraint exclusion

Sorry for asking silly question Is it possible to enforce constraint on generic in such a way that the given T can be derived from any reference Type except some A,B,C (where A,B,C are reference types). (i.e) Where T : class except A,B,C ...

error: ISO C++ forbids declaration of ‘iterator’ with no type

Hi, I am trying to build/run an old C++ system. I've already been able to built it in Ubuntu 9.10 with g++4.4 Now I'm trying to build in Ubuntu 8.04 with g++4.2. And I am getting the following errors: stringmap.h:353: erro: ISO C++ forbids declaration of ‘iterator’ with no type stringmap.h:353: erro: extra qualification ‘stringmap<_Tp>...

C#/Fixing Operators for Generics

I tried to apply operators on Generics (for my example ,multiplication) public static List<TOutput> Conversion<TOutput>(List<TInput> input) { List<TOutput> outList = new List<TOutput>(); foreach(TInput tinput in input) { double dbl = tinput *tinput; outList.Add(dbl); } ...

C# Generics reason for invalidity

I took this from Jon Skeet's "C# in depth". He mentioned the following is valid (1) class Sample<T> where T:class,Stream and the following is invalid (2) class Sample<T> where T:Stream,class what is the reason for the second one being invalid? ...

Generics and Class.forName

Hi, I would like to create an instance of a specified class using its name. My code is shown below. I get a compiler warning. Am I doing this the right way? Is it even possible to use the name of a class and get an instance of that type back, as I don't think there is any way of the compiler knowing what the type should be? public sta...

C# -IComparable<T> and IEquatable<T>

What is the major difference between IComparable<T> and IEquatable<T> ? When to use each one specifically? ...

C# -Generics -Open and closed constructed Types

Recently i noticed that Generics constructed types can be open and closed.But I am unable what do they actually mean.can you give a simple example? ...

How can we assume a bool Property on a Generic<T> class , where T is a auto generated LINQ2SQL class

I am doing a CRUD operation on Database generated class(using LINQ2SQL) in my WPF application. All of my DB tables have IsDelete property exists. So I want to define an abstract/interface class to do the SoftDelete(). My question here is, how can I define my Generic class in such a way as to access T.IsDelete = true ? or in code I want t...

C#: How to use generic method with "out" variable

I want to create a simple generic function void Assign<T>(out T result) { Type type = typeof(T); if (type.Name == "String") { // result = "hello"; } else if (type.Name == "Int32") { // result = 100; } else result = default(T); } Usage: int value; string text; Assign(value); // <<< should set value to 100 ...

ForEach @ Generixs

I normally see the use of,foreach @ generics as List<int> lst = new List<int>(); lst.Add(10); lst.Add(20); lst.Add(30); lst.ForEach(x => Console.WriteLine(x)); How can i achieve something similar: lst.ForEach(x => x *x ) ? ...

Problem with Java generics

I have an error in some classes when I use generics. I specified where I get the error. I can't get rid of it. I tried with casts, and everything I could think of. class UltraTable<O extends Object> { public void setContent(Collection<O> collection) { } } class ObjectA { } class AShell { protected UltraTable<? extends ObjectA> table...

whats wrong with this code? It is not filtering the collection!!!

public class CollectionsFilter { public static void main(String[] args) { List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Collection<Integer> evenNumbers = Utils.filter(list, new Predicate<Integer>() { public boolean apply(Integer i) { if (i % 2 == 0) { ...

accept supertype of generic parameter on single object?

is it possible somehow to accept only super types of the generic Type of a class? what im looking for is something like: class <T extends Object> MyClass { public <TS super T> void myMethod(TS someObjectSuperToTheClass) { //do stuff } } i don't really need it anymore (and its probably not all that usefull to begin with) but I'm ...

MissingMemberException when calling generic method in C#

When running a unittest, I receive the following Exception: Test method threw exception: System.MissingMethodException: Method not found: 'System.String DataUtilities.HelperMethods.Serialize(!!0)'.. This only happens when running the unittest and not when debugging it. When I debug it, no error occurs. The signature of the...

Access Known Class's Parameter Named After Generic Class

I am trying to create a generic base repository for my Linq2Sql entities. I'd like to implement a generic FindAll() method like so. class BaseRepository<T> : IBaseRepository<T> { private readonly FooDataContext _ctx = new FooDataContext(); public IQueryable<T> FindAll() { return _ctx.T; } public void Add(T ...

Java class level type inference

I know the code below doesn't compile, and yes I might be crazy. What I'm trying to do is slip an implementation (B below) of a abstract class (A) as the super class of class (C) that also extends the abstract class. I'd also like to defer method override to the super class. Something like... abstract class A { abstract Object get(...

Adding a parameter to a FindAll for a Generic List in VB.NET

Excellent question and useful looking answers at: http://stackoverflow.com/questions/731337/adding-a-parameter-to-a-findall-for-a-generic-list-in-c But can anyone help turn Jon Skeet's assistance into valid .NET 2.0 VB? I have run his answers through a couple of the usual CSharp converters but the results don't compile. ...

C#: cast to generic interface with base type

Here's the code: public interface IValidator<T> { bool IsValid(T obj); } public class OrderValidator: IValidator<Order> { // ... } public class BaseEntity { } public class Order: BaseEntity { } The problem is that I can't do: var validator = new OrderValidator(); // this line throws because type can't be converted var baseVal...