It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way.
List<string> sl = new List<string>();List<object> ol;ol = sl;
results in Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.Generic.List’
And then...
List<string> sl = new List<string...
I'm trying to set up an inheritance hierarchy similar to the following:
abstract class Vehicle
{
public string Name;
public List<Axle> Axles;
}
class Motorcycle : Vehicle
{
}
class Car : Vehicle
{
}
abstract class Axle
{
public int Length;
public void Turn(int numTurns) { ... }
}
class MotorcycleAxle : Axle
{
public bool W...
I'm having some inheritance issues as I've got a group of inter-related abstract classes that need to all be overridden together to create a client implementation. Ideally I would like to do something like the following:
abstract class Animal
{
public Leg GetLeg() {...}
}
abstract class Leg { }
class Dog : Animal
{
public overrid...
I discovered that the cause of my problems (see question) was the lack of support in C# for covariance on inherited methods' return types. Now I'm curious what languages support this feature. Accepted answer will be whoever can name the most.
EDIT: John Millikin correctly pointed out that lots of dynamic languages support this. I'll ...
Hi,
Consider these classes,
class Base
{
...
};
class Derived : public Base
{
...
};
this function
void BaseFoo( std::vector<Base*>vec )
{
...
}
And finally my vector
std::vector<Derived*>derived;
I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector...
Hello,
As far as i know it is not possible to do the following in C# 2.0
public class Father
{
public virtual Father SomePropertyName
{
get
{
return this;
}
}
}
public class Child : Father
{
public override Child SomePropertyName
{
get
{
return this;...
I'm trying to show someone a use for interfaces in a crazy situation they've created. They have several unrelated objects in lists, and need to perform an operation on two string properties in each object. I'm pointing out that if they define the properties as part of an interface, they can use the interface object as the type for a me...
I have code like this:
class RetInterface {...}
class Ret1: public RetInterface {...}
class AInterface
{
public:
virtual boost::shared_ptr<RetInterface> get_r() const = 0;
...
};
class A1: public AInterface
{
public:
boost::shared_ptr<Ret1> get_r() const {...}
...
};
This code does not compile. In visual stu...
I've got an error in my build which says:
Error 12 Cannot implicitly convert
type
'System.Collections.Generic.IEnumerator< BaseClass>'
to
'System.Collections.Generic.IEnumerator< IParentClass>'.
An explicit conversion exists (are you
missing a cast?)
Is it wrong to simply cast it away?
This is my code:
public Dictiona...
I didn't attend PDC 2008, but I heard some news that C# 4.0 is announced to support Generic covariance and contra-variance. That is, List<string> can be assigned to List<object>. How could that be?
In Jon Skeet's book C# in Depth, it is explained why C# generics doesn't support covariance and contra-variance. It is mainly for writing se...
Can someone explain why there is the need to add an out or in parameter to indicate that a generic type is Co or Contra variant in C# 4.0?
I've been trying to understand why this is important and why the compiler can't just figure it out..
Thanks,
Josh
...
What are the concepts of covariance and contravariance?
Given 2 classes, Animal and Elephant (which inherits from Animal), my understanding is that you get runtime errors in .NET if you try and put an Elephant into an array of Animal, which happens because Elephant is "bigger" (more specific) than Animal. But could you assign Animal to...
Hi,
I know that C# supports covariance in arrays like this :
object[] array = new string[3];
But I'm getting an error when it tries to compile the below code
class Dummy<K,T> where T:K
{
public void foo()
{
K[] arr = new T[4];
}
}
It says "Cannot implicitly convert type 'T[]' to 'K[]' "
Why I'm getting this er...
Given this magical interface:
public interface IHat<out TRabbit>
{
TRabbit Take();
}
And this class hierarchy:
public class Rabbit { }
public class WhiteRabbit : Rabbit { }
I can now compile this:
IHat<WhiteRabbit> hat1 = null;
IHat<Rabbit> hat2 = hat1;
Which is great. But what if I define the interface differently:
public...
I feel like this one has been asked before, but I'm unable to find it on SO, nor can I find anything useful on Google. Maybe "covariant" isn't the word I'm looking for, but this concept is very similar to covariant return types on functions, so I think it's probably correct. Here's what I want to do and it gives me a compiler error:
c...
In Java I might do this:
class MyClass {
private List<? extends MyInterface> list;
public void setList(List<MyImpl> l) { list = l; }
}
...assuming that (MyImpl implements MyInterface) of course.
What is the analog for this in Scala, when using a Buffer?
import java.lang.reflect._
import scala.collection.mutable._
class Sca...
Following on from this question, can someone explain the following in Scala:
class Slot[+T] (var some: T) {
// DOES NOT COMPILE
// "COVARIANT parameter in CONTRAVARIANT position"
}
I understand the distinction between T+ and T in the type declaration (it compiles if I use T). But then how does one actually write a class whi...
EDIT: Re-written this question based on original answer
The scala.collection.immutable.Set class is not covariant in its type parameter. Why is this?
import scala.collection.immutable._
def foo(s: Set[CharSequence]): Unit = {
println(s)
}
def bar(): Unit = {
val s: Set[String] = Set("Hello", "World");
foo(s); //DOES NOT CO...
I have asked quite a few questions about the Scala collection types and how they might actually be used. Consider how I might write some service API/implementation in Java:
public interface JavaProductAPI {
public Set<IProduct> getProducts(String isin);
}
And now the impl:
public class JavaProductAPIImpl implements JavaProductAP...
In my application I am creating a simple event hub that offers something for registering subscribers:
Subscribes<EventType>(ISubscriber<EventType> subscriber)
// and some other methods for adding subscribers
And for publishing events.
Publish<EventType>(EventType @event)
Quite simple. I want to route Publish<int>(0) to all subscri...