Not entirely sure how to phrase the question, because it's a "why doesn't this work?" type of query.
I've reduced my particular issue down to this code:
public interface IFoo
{
}
public class Foo : IFoo
{
}
public class Bar<T> where T : IFoo
{
public Bar(T t)
{
}
public Bar()
: this(new Foo()) // cannot conve...
This really has my stumped today. I'm sure its simple, but... Here is my sample code:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public ArrayList SomeProp { get; set; }
static void Main(string[] args)
{
// Get the Type of a property by reflection.
Type myP...
I would like to be able to do somthing like the following:
//non-generic
var MyTable = new Table();
string name = MyTable.Name;
IEnumerable<String> rows = MyTable.Rows;
//generic
var MyTableGeneric = new Table<MyType>();
string name = MyTableGeneric.Name;
IEnumerable<MyType> rows = MyTableGeneric .Rows;
Would something like this be t...
I'm sure I've done this before, but can't find any example of it! Grrr...
For example, I want to convert an IList<T> into a BindingList<T>:
public class ListHelper
{
public static BindingList<T> ToBindingList(IList<T> data)
{
BindingList<T> output = new BindingList<T>();
foreach (T item in data)
out...
I have been struggling for some time trying to define a generic interface, but I fail to
achieve what I want. The following is a simplified example of the problem.
Let's say I have a generic Message class
public class Message<T> {
private T content;
public void setContent(T content) {
this.content = content;
}
p...
Does Krzysztof's recommendation apply to constructors? If so, how do you implement it properly?
We recommend using Collection, ReadOnlyCollection, or KeyedCollection for outputs and properties and interfaces IEnumerable, ICollection, IList for inputs.
For example,
public ClassA
{
private Collection<String> strings;
publi...
I'm allocating an array of T, T extends Number inside a class. I can do it this way:
myClass test = new myClass(Double.class, 20);
Then the constructor itself:
myClass(Class<T> type, size)
{
array = (T[]) Array.newInstance(type, size);
}
I'd like to know if it's possible to do it like this:
myClass(Number n, size)
{
array ...
Hi,
I've got a problem with inheritance and generics.
This is the code that illustrates my problem:
namespace TestApplication
{
public class MyClass<T>
{
private T field;
public MyClass(T field)
{
this.field = field;
}
}
public class MyIntClass : MyClass<int>
{
pu...
How can I do the following in C#? What is the right way to write the first line of this code snippet?
using KVP<K, V> = System.Collections.Generic.KeyValuePair<K, V>;
class C { KVP<int, string> x; }
...
Hello all,
I have a generic list of messages, which I pass to a method by reference.
The method uses one of the messages from the list and updates the message.
How do I get this message updated with a new text, when passing the entire list by reference?
e.g.
private int RetrieveAndProcessQueueEntityRows(
string sEntityCode,...
I want to do this:
MethodInfo m = myList.GetType().GetMethod("ConvertAll", System.Reflection.BindingFlags.InvokeMethod).MakeGenericMethod(typeof(object));
List<object> myConvertedList = (List<object>)m.Invoke(myList, new object[]{ (t => (object)t)});
myList is a generic list of a specific type (unknown to the application), and I want ...
Are there any solutions or tools that transform java 5 code that uses all the new java 5 features(generics, autoboxing, varargs, static imports) into code that can run on a J2ME device? I am particularly interested in handling generics - the other features are nice to have but not mandatory.
I need source code as the result of this conv...
How come, in Java, I can write
List<?> list = new LinkedList<Double>();
but not
List<Container<?>> list = new LinkedList<Container<Double>>();
where Container is just something like
public class Container<T> { ... }
This came up because I have a method that accepts a List<Container<?>>, and I would like to use Arrays.asList to p...
For example I have a simple class like
public class Person{
public int Age {get;set;}
public string Name {get;set;}
}
I need to make a method that takes any class and spits out values of properties set in the object as a string in format "Age:35;Name:John Doe;"
I am looking for a method signature on the lines of
public str...
First off, according to http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx, the List.Find method is only listed as throwing ArgumentNullException. However I have the following test code which, when using Find with an anonymous delegate, throws a NullReferenceException when the object being searched for is not found.
namespace MyTest...
I have a legacy class that the class itself is not a generic but one of its methods return type uses generics:
public class Thing {
public Collection<String> getStuff() { ... }
}
getStuff() uses generics to return a collection of strings. Therefore I can iterate over getStuff() and there's no need to cast the elements to a String:...
So I have a generic list, and an oldIndex and a newIndex value.
I want to move the item at oldIndex, to newIndex...as simply as possible.
Any suggestions?
Note
The item should be end up between the items at (newIndex - 1) and newIndex before it was removed.
...
My class has the following core:
class SmartDbConnection
{
private readonly IDbConnection Connection;
public SmartDbConnection(string ConnectionString)
{
if(ConnectionString.Contains("MultipleActiveResultSets=true"))
{
Connection = new SqlConnection(ConnectionString);
}
}
}
I don't want it to have "SqlConnection" hardcod...
In Java 1.4.2, class java.math.BigInteger implements interfaces Comparable, Serializable.
In Java 1.5.0, class java.math.BigInteger implements interfaces Serializable, Comparable<BigInteger>.
This is just an example to help me ask about < and >. What I am really wondering about is the < and > stuff.
My question is threefold: What doe...
Consider this example (typical in OOP books):
I have an Animal class, where each Animal can have many friends.
And subclasses like Dog, Duck, Mouse etc which add specific behavior like bark(), quack() etc.
Here's the Animal class:
public class Animal {
private Map<String,Animal> friends = new HashMap<String,Animal>();
public v...