Click here to Skip to main content
16,012,223 members
Home / Discussions / C#
   

C#

 
AnswerRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM697-Nov-11 11:11
professionalDaveyM697-Nov-11 11:11 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
BillWoodruff7-Nov-11 19:03
professionalBillWoodruff7-Nov-11 19:03 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM697-Nov-11 21:36
professionalDaveyM697-Nov-11 21:36 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
Pete O'Hanlon7-Nov-11 23:19
mvePete O'Hanlon7-Nov-11 23:19 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM698-Nov-11 6:45
professionalDaveyM698-Nov-11 6:45 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
BobJanova8-Nov-11 0:14
BobJanova8-Nov-11 0:14 
GeneralRe: "economics" of casting to an interface in a parameter declaration of a method ? Pin
DaveyM698-Nov-11 6:57
professionalDaveyM698-Nov-11 6:57 
AnswerNot so simple (Re: "economics" of casting to an interface in a parameter declaration of a method?) Pin
Sergey Alexandrovich Kryukov8-Nov-11 14:58
mvaSergey Alexandrovich Kryukov8-Nov-11 14:58 
Hi Bill,

To start with, let me note that the implicit cast from an instance of implementing class to the interface reference is not always possible. Consider this:

C#
public class SimpleClass : TheInterface {
    string TheInterface.iText { get; set; }
    void Test() {
        TheInterface intrf = this; //implicit cast works
        intrf.iText = "will work";
        //this.iText = "won't work";
        ((TheInterface)this).iText = "will work";
    } //Test
} //class SimpleClass


Please pay attention: I switched to explicit implementation of interface member TheInterface.iText and eliminated public to make this experiment pure and possible. If I worked with implicit implementation and public, the result would be difficult (all forms would work), just because there are two different "views" of iText: in is a member of TheInterface, another one — a (public) member of SimpleClass. Physically this is the same but those are different things from the access standpoint. With explicit implementation, actual assignment would be done with the member of SimpleClass. Of course, the same story goes with regular methods.

[EDIT]

I would recommend to prefer explicit interface implementation over implicit one. It has additional benefits in many cases. (One of them — a possibility to have more than one indexed ("this") properties (of different signatures).)

[END EDIT]

In a next step, I'll try to convince you that a cast to interface reference may be not so trivial. To understand that, let's consider the weak form of multiple inheritance:

C#
internal interface IFirst {
     string A { get; set; }
     int B { get; set; }
 } //interface IFirst

 internal interface ISecond {
     string C { get; set; }
     double D { get; set; }
 } //interface ISecond

 internal class Implementation : IFirst, ISecond {
     string IFirst.A { get; set; }
     int IFirst.B { get; set; }
     string ISecond.C { get; set; }
     double ISecond.D { get; set; }
 } //class Implementation


How this can be implemented when you cast the instance of Implementation to first IFirst and ISecond? The "physical" references should be different. I don't know exact memory layout of the instance, but we can imagine it based on some C++ experience:

[Reference to TypeOf(Implementation)]
...
other methods
[Reference to TypeOf(IFirst)] -- reference to the code of IFirst points here
...
IFirst.A.Get
IFirst.A.Set
IFirst.B.Get
IFirst.B.Set
[Reference to TypeOf(ISecond)] -- reference to the code of ISecond points here
...
ISecond.C.Get
ISecond.C.Set
ISecond.D.Get
ISecond.D.Set
...


The above diagram is similar to the Virtual Method Table. The reference to any object is a double "pointer": 1) to the area in memory where the instance data starts; 2) to the area in memory where the addresses of the methods start, 3) everything else.

The "pointer" (1) is different for different instances of the class, and the "pointer" (2) is the same per type, but it should be different if the implementing class is the same but it is cast to one or another interface.

"Pointer" is taken in quote to show its difference with .NET "references" which allow objects to be relocated in a way transparent for applications.

This is actually my fantasy, but it's based on my study of memory layout for C++ and Delphi implementing COM interfaces. Otherwise, how could it work? All method addresses should be dispatched during run time; and dispatching mechanism should have good performance, so all called via dynamic dispatch should simply take the table of methods (it could be different from mine) and operate the offset relative to the start of interface, because only the interface reference is provided at the point of the call.

So, I think that the cast to different interfaces could involve the reference "shift" and associated cost. This cost could be so small though so it might be not detectable. I'm not 100% sure those as the dispatching mechanism could be slightly different from that of COM or anything else I know. It would be interesting to study it in detail.

Thank you.
—SA
Sergey A Kryukov


modified 8-Nov-11 22:40pm.

QuestionOmar Gameel, Apriori Algorithm Please Help..!!! Pin
Member 78679836-Nov-11 11:29
Member 78679836-Nov-11 11:29 
GeneralRe: Omar Gameel, Apriori Algorithm Please Help..!!! Pin
harold aptroot6-Nov-11 11:41
harold aptroot6-Nov-11 11:41 
AnswerRe: Omar Gameel, Apriori Algorithm Please Help..!!! Pin
Dalek Dave6-Nov-11 12:54
professionalDalek Dave6-Nov-11 12:54 
Questionauto check DataGridViewCheckBoxColumn Pin
Danzy836-Nov-11 10:52
Danzy836-Nov-11 10:52 
AnswerRe: auto check DataGridViewCheckBoxColumn Pin
Dalek Dave6-Nov-11 12:57
professionalDalek Dave6-Nov-11 12:57 
AnswerRe: auto check DataGridViewCheckBoxColumn Pin
Dr.Walt Fair, PE6-Nov-11 16:59
professionalDr.Walt Fair, PE6-Nov-11 16:59 
AnswerRe: auto check DataGridViewCheckBoxColumn Pin
purnananda behera7-Nov-11 19:00
purnananda behera7-Nov-11 19:00 
QuestionHow to read an Autocad file with c# Pin
JUSTLOVE16-Nov-11 10:04
JUSTLOVE16-Nov-11 10:04 
AnswerRe: How to read an Autocad file with c# Pin
Richard MacCutchan6-Nov-11 10:19
mveRichard MacCutchan6-Nov-11 10:19 
GeneralRe: How to read an Autocad file with c# Pin
Dalek Dave6-Nov-11 12:57
professionalDalek Dave6-Nov-11 12:57 
GeneralRe: How to read an Autocad file with c# Pin
Richard MacCutchan6-Nov-11 22:28
mveRichard MacCutchan6-Nov-11 22:28 
AnswerRe: How to read an Autocad file with c# Pin
Luc Pattyn6-Nov-11 22:58
sitebuilderLuc Pattyn6-Nov-11 22:58 
GeneralRe: How to read an Autocad file with c# Pin
Richard MacCutchan6-Nov-11 23:27
mveRichard MacCutchan6-Nov-11 23:27 
GeneralRe: How to read an Autocad file with c# Pin
Luc Pattyn6-Nov-11 23:41
sitebuilderLuc Pattyn6-Nov-11 23:41 
GeneralRe: How to read an Autocad file with c# Pin
Dennis E White7-Nov-11 6:11
professionalDennis E White7-Nov-11 6:11 
QuestionDownload image from URL Pin
eddjusted6-Nov-11 2:02
eddjusted6-Nov-11 2:02 
AnswerRe: Download image from URL Pin
Hanzaplast6-Nov-11 2:16
Hanzaplast6-Nov-11 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.