lets say we have a 'shape' class which doesn't implement IComparable
interface..
compiler doesn't give you error for the lines below..
shape b= new shape();
IComparable h;
h=(IComparable)b;
but it complains for the following lines
shape b= new shape();
int a;
a=(int)b;
error is Cannot convert type 'shape' to 'int'
is there difference between casting to a class and casting to an interface
?? what does the compiler checks when it is casting to an interface (or does
it check anything ?)
Thanx a lot,
yufufi 7 3544
that is a very good shout.
I was under the impression that you could only cast an object to an
interface if said object implementated said interface.
After having a play I see (like you) that this is not true.
It seems that the compiler does not type check a cast to an interface
(whether this is by design or not I do not know?)
However in the runtime the cast is succeeding.
Unfortunately the methods exposed by the interface will not succeed if
accessed because of the incompatible object type -so when you try and run
one through the interface ...BANG! exception.
I can see this functionality of being of some benefit in some instances, but
I would love to know whether this is by design.
Nice post, well done!
--
Br,
Mark Broadbent
mcdba , mcse+i
=============
"yufufi" <yu****@ttnet.net.tr> wrote in message
news:OG**************@tk2msftngp13.phx.gbl... lets say we have a 'shape' class which doesn't implement IComparable interface..
compiler doesn't give you error for the lines below..
shape b= new shape(); IComparable h; h=(IComparable)b;
but it complains for the following lines shape b= new shape(); int a; a=(int)b;
error is Cannot convert type 'shape' to 'int'
is there difference between casting to a class and casting to an interface ?? what does the compiler checks when it is casting to an interface (or
does it check anything ?)
Thanx a lot,
yufufi
If the type of your object is not sealed, the compiler can't know at compile
time whether the actual object implements the "IComparable" interface or
not. Remeber, at runtime the variable of type "shape" may point to an object
of a type that is derived from "shape" and implements "IComparable".
However - as there is no multiple inheritance - it is not possible that
there is a class that is derived from both "Shape" and "Int".
These are the exact rules, copied from the C# language specification:
"The explicit reference conversions are:
- From object to any other reference-type.
- From any class-type S to any class-type T, provided S is a base class of
T.
- From any class-type S to any interface-type T, provided S is not sealed
and provided S does not implement T.
- From any interface-type S to any class-type T, provided T is not sealed or
provided T implements S.
- From any interface-type S to any interface-type T, provided S is not
derived from T.
- From an array-type S with an element type SE to an array-type T with an
element type TE, provided all of the -following are true:
- S and T differ only in element type. In other words, S and T have the
same number of dimensions.
- Both SE and TE are reference-types.
- An explicit reference conversion exists from SE to TE.
- From System.Array and the interfaces it implements to any array-type.
- From System.Delegate and the interfaces it implements to any
delegate-type. "
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in
news:OG**************@tk2msftngp13.phx.gbl... lets say we have a 'shape' class which doesn't implement IComparable interface..
compiler doesn't give you error for the lines below..
shape b= new shape(); IComparable h; h=(IComparable)b;
but it complains for the following lines shape b= new shape(); int a; a=(int)b;
error is Cannot convert type 'shape' to 'int'
is there difference between casting to a class and casting to an interface ?? what does the compiler checks when it is casting to an interface (or
does it check anything ?)
Thanx a lot,
yufufi
hey Niki, thanks for that too. Always good to have exact references. Cheers.
--
Br,
Mark Broadbent
mcdba , mcse+i
=============
"Niki Estner" <ni*********@cube.net> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl... If the type of your object is not sealed, the compiler can't know at
compile time whether the actual object implements the "IComparable" interface or not. Remeber, at runtime the variable of type "shape" may point to an
object of a type that is derived from "shape" and implements "IComparable". However - as there is no multiple inheritance - it is not possible that there is a class that is derived from both "Shape" and "Int".
These are the exact rules, copied from the C# language specification: "The explicit reference conversions are: - From object to any other reference-type. - From any class-type S to any class-type T, provided S is a base class of T. - From any class-type S to any interface-type T, provided S is not sealed and provided S does not implement T. - From any interface-type S to any class-type T, provided T is not sealed
or provided T implements S. - From any interface-type S to any interface-type T, provided S is not derived from T. - From an array-type S with an element type SE to an array-type T with an element type TE, provided all of the -following are true: - S and T differ only in element type. In other words, S and T have the same number of dimensions. - Both SE and TE are reference-types. - An explicit reference conversion exists from SE to TE. - From System.Array and the interfaces it implements to any array-type. - From System.Delegate and the interfaces it implements to any delegate-type. "
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in news:OG**************@tk2msftngp13.phx.gbl... lets say we have a 'shape' class which doesn't implement IComparable interface..
compiler doesn't give you error for the lines below..
shape b= new shape(); IComparable h; h=(IComparable)b;
but it complains for the following lines shape b= new shape(); int a; a=(int)b;
error is Cannot convert type 'shape' to 'int'
is there difference between casting to a class and casting to an
interface ?? what does the compiler checks when it is casting to an interface (or does it check anything ?)
Thanx a lot,
yufufi
Nice, but then I have another question..
In generics you can check the type of the input like this:
class Box<T>: where T:IComparable
but compiler gives an error at compile time if you try to create Box
instance of type shape.
On the other hand if you try
class Box<T>
{
public int Compare(T inp1,T inp2)
{
return ((IComparable)inp1).CompareTo(inp2);
}
}
it compiles very well ??
Thanx,
yufufi
"Niki Estner" <ni*********@cube.net> wrote in message
news:Oi**************@TK2MSFTNGP12.phx.gbl... If the type of your object is not sealed, the compiler can't know at
compile time whether the actual object implements the "IComparable" interface or not. Remeber, at runtime the variable of type "shape" may point to an
object of a type that is derived from "shape" and implements "IComparable". However - as there is no multiple inheritance - it is not possible that there is a class that is derived from both "Shape" and "Int".
These are the exact rules, copied from the C# language specification: "The explicit reference conversions are: - From object to any other reference-type. - From any class-type S to any class-type T, provided S is a base class of T. - From any class-type S to any interface-type T, provided S is not sealed and provided S does not implement T. - From any interface-type S to any class-type T, provided T is not sealed
or provided T implements S. - From any interface-type S to any interface-type T, provided S is not derived from T. - From an array-type S with an element type SE to an array-type T with an element type TE, provided all of the -following are true: - S and T differ only in element type. In other words, S and T have the same number of dimensions. - Both SE and TE are reference-types. - An explicit reference conversion exists from SE to TE. - From System.Array and the interfaces it implements to any array-type. - From System.Delegate and the interfaces it implements to any delegate-type. "
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in news:OG**************@tk2msftngp13.phx.gbl... lets say we have a 'shape' class which doesn't implement IComparable interface..
compiler doesn't give you error for the lines below..
shape b= new shape(); IComparable h; h=(IComparable)b;
but it complains for the following lines shape b= new shape(); int a; a=(int)b;
error is Cannot convert type 'shape' to 'int'
is there difference between casting to a class and casting to an
interface ?? what does the compiler checks when it is casting to an interface (or does it check anything ?)
Thanx a lot,
yufufi
Actually I don't know too much about .net generics yet, but I guess they
apply the implicit casting rules here.
After all, the whole fun about generics is that you don't have the runtime
overhead for casting/boxing, and that the code is safer because invalid cast
exceptions are no longer possible, like they used to be in 'pseudo-generic
containers' that used System.Object references everywhere.
If you were allowed to instantiate a "Box<T> where I:IComparable" with a
type T that doesn't implement that interface, all these advantages would be
lost, wouldn't they?
Not sure if that's the whole reason, but it sounds reasonable to me.
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in
news:ur**************@TK2MSFTNGP11.phx.gbl... Nice, but then I have another question..
In generics you can check the type of the input like this:
class Box<T>: where T:IComparable
but compiler gives an error at compile time if you try to create Box instance of type shape.
On the other hand if you try
class Box<T> { public int Compare(T inp1,T inp2) { return ((IComparable)inp1).CompareTo(inp2); } }
it compiles very well ??
Thanx,
yufufi "Niki Estner" <ni*********@cube.net> wrote in message news:Oi**************@TK2MSFTNGP12.phx.gbl... If the type of your object is not sealed, the compiler can't know at compile time whether the actual object implements the "IComparable" interface or not. Remeber, at runtime the variable of type "shape" may point to an object of a type that is derived from "shape" and implements "IComparable". However - as there is no multiple inheritance - it is not possible that there is a class that is derived from both "Shape" and "Int".
These are the exact rules, copied from the C# language specification: "The explicit reference conversions are: - From object to any other reference-type. - From any class-type S to any class-type T, provided S is a base class
of T. - From any class-type S to any interface-type T, provided S is not
sealed and provided S does not implement T. - From any interface-type S to any class-type T, provided T is not
sealed or provided T implements S. - From any interface-type S to any interface-type T, provided S is not derived from T. - From an array-type S with an element type SE to an array-type T with
an element type TE, provided all of the -following are true: - S and T differ only in element type. In other words, S and T have
the same number of dimensions. - Both SE and TE are reference-types. - An explicit reference conversion exists from SE to TE. - From System.Array and the interfaces it implements to any array-type. - From System.Delegate and the interfaces it implements to any delegate-type. "
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in news:OG**************@tk2msftngp13.phx.gbl... lets say we have a 'shape' class which doesn't implement IComparable interface..
compiler doesn't give you error for the lines below..
shape b= new shape(); IComparable h; h=(IComparable)b;
but it complains for the following lines shape b= new shape(); int a; a=(int)b;
error is Cannot convert type 'shape' to 'int'
is there difference between casting to a class and casting to an interface ?? what does the compiler checks when it is casting to an interface
(or does it check anything ?)
Thanx a lot,
yufufi
It sounds reasonable to me too , but it also shouldn' allow
class Box<T>
{
public int Compare(T inp1,T inp2)
{
return ((IComparable)inp1).CompareTo(inp2);
}
}
Box<shape> a=new Box<shape>();
I think there is a contradiction here. compiler doesn't give an error here
since shape may be a pointer to a class which is derived from shape and
implements IComparable. But when you use "where T:IComparable" , it doesn't
think maybe I'll pass a class which is also derived from shape and
implements IComparable.
"Niki Estner" <ni*********@cube.net> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl... Actually I don't know too much about .net generics yet, but I guess they apply the implicit casting rules here. After all, the whole fun about generics is that you don't have the runtime overhead for casting/boxing, and that the code is safer because invalid
cast exceptions are no longer possible, like they used to be in 'pseudo-generic containers' that used System.Object references everywhere. If you were allowed to instantiate a "Box<T> where I:IComparable" with a type T that doesn't implement that interface, all these advantages would
be lost, wouldn't they?
Not sure if that's the whole reason, but it sounds reasonable to me.
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in news:ur**************@TK2MSFTNGP11.phx.gbl... Nice, but then I have another question..
In generics you can check the type of the input like this:
class Box<T>: where T:IComparable
but compiler gives an error at compile time if you try to create Box instance of type shape.
On the other hand if you try
class Box<T> { public int Compare(T inp1,T inp2) { return ((IComparable)inp1).CompareTo(inp2); } }
it compiles very well ??
Thanx,
yufufi "Niki Estner" <ni*********@cube.net> wrote in message news:Oi**************@TK2MSFTNGP12.phx.gbl... If the type of your object is not sealed, the compiler can't know at compile time whether the actual object implements the "IComparable" interface
or not. Remeber, at runtime the variable of type "shape" may point to an object of a type that is derived from "shape" and implements "IComparable". However - as there is no multiple inheritance - it is not possible
that there is a class that is derived from both "Shape" and "Int".
These are the exact rules, copied from the C# language specification: "The explicit reference conversions are: - From object to any other reference-type. - From any class-type S to any class-type T, provided S is a base
class of T. - From any class-type S to any interface-type T, provided S is not sealed and provided S does not implement T. - From any interface-type S to any class-type T, provided T is not sealed or provided T implements S. - From any interface-type S to any interface-type T, provided S is not derived from T. - From an array-type S with an element type SE to an array-type T with an element type TE, provided all of the -following are true: - S and T differ only in element type. In other words, S and T have the same number of dimensions. - Both SE and TE are reference-types. - An explicit reference conversion exists from SE to TE. - From System.Array and the interfaces it implements to any
array-type. - From System.Delegate and the interfaces it implements to any delegate-type. "
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in news:OG**************@tk2msftngp13.phx.gbl... > lets say we have a 'shape' class which doesn't implement IComparable > interface.. > > compiler doesn't give you error for the lines below.. > > shape b= new shape(); > IComparable h; > h=(IComparable)b; > > but it complains for the following lines > shape b= new shape(); > int a; > a=(int)b; > > error is Cannot convert type 'shape' to 'int' > > is there difference between casting to a class and casting to an
interface > ?? what does the compiler checks when it is casting to an interface (or does > it check anything ?) > > Thanx a lot, > > yufufi > > > > > > > >
I don't think it's a contradiction;
If you write:
int i = ...;
IComparable comp;
comp = i;
this will compile, as the compiler can check at compile time that it "i"
does imlpement the interface IComparable. However, something like:
object o = ...;
IComparable comp;
comp = o;
will not, as the cast may work at runtime, but doesn't neccessarily.
However, if you use an explicit cast like:
comp = (IComparable) o;
the compiler "assumes" that you know what you are doing, and, as whatever o
points to might well implement ths IComparable interface does not issue an
error.
The main difference is between "static type checking", that is checking the
type of the declared variable at compile time, and "dynamic type checking",
that is checking the type of the referenced object at runtime. Static casts
are the "safer" ones, as they can never fail. So, for implicit casts and
generics "static conversion rules" apply. If you need a runtime type-check,
you have to explicitly use a cast, or an is/as operator. But of course, if
the compiler can tell that a dynamic cast will definitely never succeed
unless the variable points to "null", it can still issue an error.
I'm not really sure if this explanation makes sense to you, but the
compiler's behaviour does make sense to me.
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in
news:uq**************@TK2MSFTNGP09.phx.gbl... It sounds reasonable to me too , but it also shouldn' allow
class Box<T> { public int Compare(T inp1,T inp2) { return ((IComparable)inp1).CompareTo(inp2); } } Box<shape> a=new Box<shape>();
I think there is a contradiction here. compiler doesn't give an error here since shape may be a pointer to a class which is derived from shape and implements IComparable. But when you use "where T:IComparable" , it
doesn't think maybe I'll pass a class which is also derived from shape and implements IComparable.
"Niki Estner" <ni*********@cube.net> wrote in message news:Oo**************@TK2MSFTNGP10.phx.gbl... Actually I don't know too much about .net generics yet, but I guess they apply the implicit casting rules here. After all, the whole fun about generics is that you don't have the
runtime overhead for casting/boxing, and that the code is safer because invalid cast exceptions are no longer possible, like they used to be in
'pseudo-generic containers' that used System.Object references everywhere. If you were allowed to instantiate a "Box<T> where I:IComparable" with a type T that doesn't implement that interface, all these advantages would be lost, wouldn't they?
Not sure if that's the whole reason, but it sounds reasonable to me.
Niki
"yufufi" <yu****@ttnet.net.tr> wrote in news:ur**************@TK2MSFTNGP11.phx.gbl... Nice, but then I have another question..
In generics you can check the type of the input like this:
class Box<T>: where T:IComparable
but compiler gives an error at compile time if you try to create Box instance of type shape.
On the other hand if you try
class Box<T> { public int Compare(T inp1,T inp2) { return ((IComparable)inp1).CompareTo(inp2); } }
it compiles very well ??
Thanx,
yufufi "Niki Estner" <ni*********@cube.net> wrote in message news:Oi**************@TK2MSFTNGP12.phx.gbl... > If the type of your object is not sealed, the compiler can't know at compile > time whether the actual object implements the "IComparable"
interface or > not. Remeber, at runtime the variable of type "shape" may point to
an object > of a type that is derived from "shape" and implements "IComparable". > However - as there is no multiple inheritance - it is not possible that > there is a class that is derived from both "Shape" and "Int". > > These are the exact rules, copied from the C# language
specification: > "The explicit reference conversions are: > - From object to any other reference-type. > - From any class-type S to any class-type T, provided S is a base class of > T. > - From any class-type S to any interface-type T, provided S is not sealed > and provided S does not implement T. > - From any interface-type S to any class-type T, provided T is not sealed or > provided T implements S. > - From any interface-type S to any interface-type T, provided S is
not > derived from T. > - From an array-type S with an element type SE to an array-type T
with an > element type TE, provided all of the -following are true: > - S and T differ only in element type. In other words, S and T
have the > same number of dimensions. > - Both SE and TE are reference-types. > - An explicit reference conversion exists from SE to TE. > - From System.Array and the interfaces it implements to any array-type. > - From System.Delegate and the interfaces it implements to any > delegate-type. " > > Niki > > "yufufi" <yu****@ttnet.net.tr> wrote in > news:OG**************@tk2msftngp13.phx.gbl... > > lets say we have a 'shape' class which doesn't implement
IComparable > > interface.. > > > > compiler doesn't give you error for the lines below.. > > > > shape b= new shape(); > > IComparable h; > > h=(IComparable)b; > > > > but it complains for the following lines > > shape b= new shape(); > > int a; > > a=(int)b; > > > > error is Cannot convert type 'shape' to 'int' > > > > is there difference between casting to a class and casting to an interface > > ?? what does the compiler checks when it is casting to an
interface (or > does > > it check anything ?) > > > > Thanx a lot, > > > > yufufi > > > > > > > > > > > > > > > > > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris |
last post by:
I created a control that derives from the System.Windows.Forms.Control
namespace.
I then created an interface to which this new control must adhere, so that I
could require future controls to...
|
by: cv |
last post by:
Hi, I want to create an object from a class I load from an assembly.
Then I want to cast that object to a class (an interface) 'known' to the
program in order to pass it to other methods that work...
|
by: Greg Conely via .NET 247 |
last post by:
I am creating a application that will be using plugins. I am doing this so that when I want to let this application work with another type of dbase system, I only have to write\install one plugin,...
|
by: Enrique Bustamante |
last post by:
Casting arrays that works on watch and command window but not in code.
My application is casting arrays in a way it should work. To test if I
was doing something invalid, I wrote a test code that...
|
by: JimM |
last post by:
I am trying to create a method in VS 2003 that validates an object argument
is of the proper type and within a range of values.
I am trying to use a Type to define the casting and object type for...
|
by: harvie wang |
last post by:
Hi,
I want to implement a common Form with special interface, such as MovePoint(double,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterface
{
void...
|
by: pitdog |
last post by:
This may not be the group to post this in. However I will try...
I am interested in how the .NET runtime actually handles casting. For
instance...
This will work if you assume ChildProduct is...
|
by: Tigger |
last post by:
I have an object which could be compared to a DataTable/List which I am
trying to genericify.
I've spent about a day so far in refactoring and in the process gone
through some hoops and hit some...
|
by: Ajeet |
last post by:
hi
I am having some difficulty in casting using generics. These are the
classes.
public interface IProvider<PROF>
where PROF : IProviderProfile
{
//Some properties/methods
}
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
|
by: F22F35 |
last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...
| |