generics

Generic Generics in Managed C++

I want to create a List of KeyValuePairs in a managed C++ project. Here is the syntax I'm using List<KeyValuePair<String^, String^>^>^ thing; but I'm getting the following error: error C3225: generic type argument for 'T' cannot be 'System::Collections::Generic::KeyValuePair ^', it must be a value type or a handle to a reference t...

Using Generics to encapsulate common method work

I've got a number of WebService methods that all include some very boilerplate code of wrapping the actual work in a try/catch/finally and performing the same tasks in the catch/finally. So as a way to encapsulate all of the shared catch/finally stuff I wrote a simple generic. This works and really eliminates a bunch of repetitive code ...

Creating a KeyValuePair in Managed C++

I have yet another managed C++ KeyValuePair question where I know what to do in C#, but am having a hard time translating to managed C++. Here is the code that does what I want to do in C#: KeyValuePair<String, String> KVP = new KeyValuePair<string, string>("this", "that"); I've reflected it into MC++ and get this: KeyValuePair<Strin...

WPF textblock binding with List<string>

does anyone know if there is a simple way to bind a textblock to a List. What I've done so far is create a listview and bind it to the List and then I have a template within the listview that uses a single textblock. what I'd really like to do is just bind the List to a textblock and have it display all the lines. In Winforms there was...

java bounded generic type definition

Hi, What's the difference between the following type definitions <E extends Number> and <? extends Number> Cheers, Don ...

Applying Extension method to generic class with generic type.

I was working with the generic class in vb.net. And it seems extension method cannot be applied to generic class without specifying the type. I have this generic class Public Class MyGeneric(Of T) 'Methods and properties go here ' ' End Class This is Ok <Extension()> _ Public Sub DoSomething(ByVal myGenericDoubleObj As ...

.net architecture: Creating a IRepository<T> generically

I am using a generic repository interface which allows me to add, find, update and delete objects of different types. I have then implemented two concrete repositories and can switch them around without changing the application. Everything is wonderfully disconnected. But I have hit a snag. The code inside my repository methods just feel...

Custom Linq Extension Syntax

I have written a function that gets a given number of random records from a list. Currently I can do something like: IEnumerable<City> cities = db.Cites.GetRandom(5); (where db is my DataContext connecting to a SQL Server DB) Currently, I have a function like this in every entity I need random records from: public partial class Cit...

C# - Multiple generic types in one list

This is probably not possible, but I have this class: public class Metadata<DataType> where DataType : struct { private DataType mDataType; } There's more to it, but let's keep it simple. The generic type (DataType) is limited to value types by the where statement. What I want to do is have a list of these Metadata objects of va...

StringBuilder extension method for appending a collection in C#

In C#, I'm trying to build an extension method for StringBuilder called AppendCollection() that would let me do this: var sb1 = new StringBuilder(); var sb2 = new StringBuilder(); var people = new List<Person>() { ...init people here... }; var orders = new List<Orders>() { ...init orders here... }; sb1.AppendCollection(people, p => p.T...

.NET Generic Set ?

Is there a generic container implementing the 'set' behaviour in .NET? I know I could just use a Dictionary<T, Object> (and possibly add nulls as values), because its keys act as a set, but I was curious if there's something ready-made. ...

Default value for generics

How do I create the default for a generic in VB? in C# I can call: T variable = default(T); How do I do this in VB? If this just returns null (C#) or nothing (vb) then what happens to value types? Is there a way to specify for a custom type what the default value is? For instance what if I want the default value to be the equivalent ...

In Java, when should I use "Object o" instead of generics?

For example, I could write either of these: class example <T> { ... public void insert (T data) { ... } } or class example { ... public void insert (Object o) { ... } } Is there a signficant difference between the 2 in terms of performance? With generics I could restrict the type of...

C# vs Java generics

I have heard that the Java implementation of Generics is not as good as the C# implementation. In that the syntax looks similar, what is it that is substandard about the Java implementation, or is it a religious point of view? ...

can I reflectively instantiate a generic type in java?

Is it possible to reflectively instantiate a generic type in Java? Using the technique described here I get an error because class tokens cannot be generic. Take the example below. I want to instantiate some subclass of Creator that implements Creator. The actual class name is passed in as a command line argument. The idea is to be able ...

List Generics and Casting

Hello everyone. I have two classes: Media and Container. I have two lists List<Media> and List<Container> I'm passing these lists to another function (one at a time); it can be one or another; what's the proper way to check for the "template" type of the list so i can call an asssociated method depending on the list type? or shoul...

Java: How to create a collection of a specific parent type and not its subtypes?

I am learning Java for a test (tomorrow) and am wondering about a question that will probably never come up but has got me curious. Is it possible to create a new collection class such as a list or something that can hold only a specific type and not its sub-types? Would I use generics to achieve this goal? ...

How to make IEnumerable<T> readonly?

Why is list1Instance and p lists in the Main method of the below code pointing to the same collection? class Person { public string FirstName = string.Empty; public string LastName = string.Empty; public Person(string firstName, string lastName) { this.FirstName = firstName; this.Las...

Compile Error With Generic Method in C#

EDIT: I found out that I can get it to compile if I cast the IMetadataType object to the TMetadata type. Why do I need to do this? EDIT #2: The "Values" property is a .NET dictionary of type <TMetadata, TData>. I have this generic method: private void FillMetadata<TMetadata, TData> (Metadata<TMetadata, TData> oMetadata) where T...

Java formal type parameter definition (Generics)

Hi, I'd like to define a generic type, whose actual type parameter can only be One of the numeric primitive wrapper classes (Long, Integer, Float, Double) String I can meet the first requirement with a definition like this public final class MyClass<T extends Number> { // Implementation omitted } But I can't figure out how to...