Say you have a group of objects you're creating to handle some XML parsing and all of them take the exact same object, XElement ... as such
public class User
{
public User(XElement xmlElement)
{
Id = xmlElement.GetElementValue("UserId");
}
public string Id { get; set; }
}
What I would like to do is a method...
I've defined the following generic class
public class ManagedClass<T> where T : ManagedClass<T>
{
static ManagedClass()
{
Manager = new ObjectManager<T>();
}
public static ObjectManager<T> Manager { get; protected set; }
public ManagedClass()
{
Manager.Add( (T)this );
}
}
The idea is that I...
What does List<?> mean, does it mean simply a list of objects of unspecified type?
Googling for the string <?> returns nothing useful (:
...
This following is from generics tutorials:
Say class R extends S,
public void addR(List<? extends S> s) {
s.add(0, new R()); // Compile-time error!
}
You should be able to figure out why the code above is disallowed. The type of the second parameter to s.add() is ? extends S -- an unknown subtype of S. Since we don't know what ty...
I'd like to be able to write the following code:
// contains 500 entries
IList<string> longListOfStrings = ...
// shorterListsOfStrings is now an array of 5 IList<string>,
// with each element containing 100 strings
IList<string>[] shorterListsOfStrings = longListOfStrings.Split(5);
To do this I have to create a generic extension met...
Here is an exemple of the situation:
public class ScheduleArea : IArea<Schedule>
{
//....
private Dictionary<int, ScheduleArea> subArea;
//....
#region IArea<Schedule> Members
public ICollection<KeyValuePair<int, IArea<Schedule>>> SubArea
{
get {
return (Collection<KeyValuePair<int, IArea<Schedule>>>)this.subArea;//Error...
Using Spring, I can get all beans of a certain type that are currently defined using this:
@Resource
private List<Foo> allFoos;
How does Spring do this? I thought type information of generics was erased at runtime. So how does Spring know about the type Foo of the list and only inject dependencies of the correct type?
To illustrate: ...
I would like to write a SparseVector[T] class where T can be a double, an int or a boolean.
The class will not be backed by an array (because I want a sparse data structure) but I have seen that when I build an empty array of an AnyVal type, the elements are initialized to the default value. For instance:
scala> new Array[Int](10)
re...
This post is in continuation of this one.
I am trying to understand if I am the only one who misses and needs the ability of a .NET generic type to inherit one of its generic parameter types.
The challenge is to collect compelling reasons in favour of this feature or, alternatively, get to know that there are none.
I will start with m...
When I have a Java generic function like:
<T> T choose(T a, T b) { }
and I call it from somewhere, how can I find out what type is inferred for T?
Edit: Type inference happens at compile time. So what I'm asking is, how do I get the compiler to tell me some information (the inferred type) it has during compilation, but that doesn't ...
I have seen two different approaches for creating generic repositories. What are differences between those two approaches (pros and cons) ?
Please diregard difference in the methods because I am interested in difference between
public interface IRepository<T> where T : class
and
public interface IRepository : IDisposable
Is th...
So I am trying to make a parameterized type that will work for anytype in Java this includes anything that is an object, and also the primitives types. How would i go about doing this?
Ok, suppose I want the target of a for-each to be a primitive, so its
for(int i : MyClass)
something like that. Is this possible to do?
...
C# - .net 3.5
I have a family of classes that inherit from the same base class.
I want a method in the base class to be invoked any time a property in a derrived class is accessed (get or set). However, I don't want to write code in each and every property to call the base class... instead, I am hoping there is a declarative way to "si...
Why can't I use a constraint of
where T : System.ValueType?
Why does Microsoft not allow this type
from being a constraint?
Example:
Why can't I do the following?
// Defined in a .Net class
public void bar<T>(T a) where T : ValueType {...}
// Defined in my class
public void foo<T>(T a) where T : ValueType
{ bar<T>(a); }
What...
Best way to illustrate my question is with this example code:
class Item {}
class Container< T > {}
class Program
{
static void DoSomething( object something )
{
if( typeof( Item ) == something.GetType() )
{
System.Console.WriteLine( "Item" );
}
else if( typeof( Container<> ) == something....
I like to separate my definitions from my implementations. I have an interface Entity:
public interface Entity<E> where E : Entity<E>
{
EntityId EntityId { get; }
bool ReadOnly { get; }
void FailIfReadOnly();
E Copy();
}
E is the actual entity type, such as Customer:
public interface Customer : Entity<Customer>
{
}...
Hi. I'm writing a generics class in C++/CLI (VS2008) to store and manage records of different kinds and I need collections to keep them before flusing them to DB/disk/etc. I was thinking in something like this:
ref class Record
{
// ...
};
generic<typename T>
where T : Record, gcnew()
public ref class Factory
{
public:
// ....f...
I need to implement a function which returns a TDictionary, without specifying the exact types. The returned value could be a TDictionary<string,Integer>, TDictionary<string,string> or TDictionary<string,Boolean>
Could I declare the function with TDictionary as result parameter:
function GetMap: TDictionary;
and then cast the return ...
Given a function as below, i can take a single table from my database and write a lambda using the Where extension method and pretty much build all the other cases using a simple wrapper method and supplying a filter.
public void getPeople(Expression<Func<tblPeople, bool>> filter, Action<List<tblPeople>> callback)
{
...
I am just starting to get to grips with generics and am (ab)using them to refactor a fairly complex section of my code (I've only been using c# for a little while but am fairly experienced in other languages).
I have an inheritance structure where my classes extend a base class. In the base class I have most of the functionality impleme...