Coke-22

hi
i would like to know why this is not possible ! !

Code Snippet

public interface INewClass
{
}

public class NewClass : INewClass
{
}

public class A<C> where C : INewClass
{
private C obj;

public A()
{
obj = new NewClass();
}
}


got error imlicit converting not possible from "NewClass" to "C".

ok i thougt letí»s try explizit cast.

Code Snippet

public interface INewClass
{
}

public class NewClass : INewClass
{
}

public class A<C> where C : INewClass
{
private C obj;

public A()
{
obj = (C)new NewClass();
}
}


got error cast from "NewClass" to "C" not possible.

why is the behaviour so, for the compiler are enough information present or not ! !


Re: Visual C# Language Generic casting

Marlon Grech

Hi there,

To make myself clear I will give you an example

imagine that you have another class that implements the INewClass

class NewClass2 : INewClass
{
}

you can call class A by saying...

A<NewClass2> classA;

yet the code that you have in the constructor (of class A<C>) is trying to create an instance of type "NewClass" and put it in a pointer of type "NewClass2"(since C in the example is set to NewClass2)..

what you should do, is to constaint your generic type with the new constraint (which mean you need to have a generic constructor) and create the instance by using the generic parameter itself....


public class A<C> where C : new()
{
private C obj;
public A()
{
obj = new C();
}
}

this should do the trick!!


hope it helps





Re: Visual C# Language Generic casting

Alan M.

I have a better explanation. The reason is NewClass implements the interface INewClass and so does your generic C. However, that does not mean that those two classes are the same.

Let me explain.

ArrayList, LinkedList, Array all implement the IList interface meaning they all are lists. However a linked list is not a list based on an array. Therefore, you cannot expect to be able to cast a linked list (with nodes) to an array list (based on an array). You could, however, cast both of those objects to an IList and use the methods defined for a list in IList.

Another example... say you had an interface called "IShape". Two classes that may implement IShape could be Triangle and Circle. However, you couldn't expect to cast a Triangle into a Circle and then get it's area using Circle's method. You could however cast it to IShape and then call it's GetArea() method (assuming IShape defines it) and then the due to the principle of polymorphism the correct method would be called for the type of your object.

in your example C is not of the type INewClass. That is where you've gone wrong. C is a class that implements INewClass. For example if you constrained your generic to extend NewClass (not the interface) I believe it would compile.

Edit: I will also mention what your code is trying to do doesn't make much sense. Why would you create a generic type, only to assign in the construct a specific type to it




Re: Visual C# Language Generic casting

Coke-22

hi,

if u make an explicit cast it also compiles, and that is courious to me why this works but without not !
beacuse there are no newer information in the code.


here

Code Snippet

public interface INewClass
{
}

public class NewClass : INewClass
{
}

public class A<C> where C : INewClass
{
private C obj;

public A()
{
obj = (C)(
INewClass)new NewClass();
}
}






Re: Visual C# Language Generic casting

Alan M.

Generics are supposed to give you compile time type safety (casts may crash at runtime). Those explicit casts of yours may crash the program... even if they compile!

for example...


String str = "Alan";
double d = 3.3423;

Object obj1 = str;

// now.. time to crash at runtime
d = (double)obj1;