Consider the following codes:
----------------------------------------------------
using System;
namespace ConsoleApplication1
{
interface IPointD
{
double X {get; set;}
double Y {get; set;}
}

struct PointD : IPointD
{
private double x;
private double y;
public PointD(double x, double y)
{ this.x = x; this.y = y;}

public double X
{
get { return x; }
set { x= value; }
}

public double Y
{
get { return y; }
set { y = value; }
}
}

class Class1
{
static void Method(IPointD point)
{
if (point != null)
{
point.X = 10;
point.Y = 10;
}
}

[STAThread]
static void Main(string[] args)
{
PointD point = new PointD(1, 1);
IPointD dPoint = (IPointD)point;
Method(dPoint);

Console.WriteLine("X = {0}, Y = {1}", point.X, point.Y);
}
}
}
---------------------------------------------------------

Why is the call to the method - Method(dPoint) not by reference?

Best regards,
Paul.

Re: Structure and Interface by Paul

Paul
Mon Aug 16 00:54:21 CDT 2004

Sorry, please first question.
static void Main(string[] args)
{
PointD point = new PointD(1, 1);
IPointD dPoint = (IPointD)point;
Method(dPoint);

Console.WriteLine("X = {0}, Y = {1}", point.X, point.Y);
Console.WriteLine("X = {0}, Y = {1}", dPoint .X, dPoint .Y);
}

The question should be, why is dPoint modified, but point is not?

Does
IPointD dPoint = (IPointD)point;

makes a copy of the point?

Best regards,
Paul.

"Paul Selormey" <paul@toolscenter.org> wrote in message
news:ua2TON1gEHA.3612@TK2MSFTNGP12.phx.gbl...
> Consider the following codes:
> ----------------------------------------------------
> using System;
> namespace ConsoleApplication1
> {
> interface IPointD
> {
> double X {get; set;}
> double Y {get; set;}
> }
>
> struct PointD : IPointD
> {
> private double x;
> private double y;
> public PointD(double x, double y)
> { this.x = x; this.y = y;}
>
> public double X
> {
> get { return x; }
> set { x= value; }
> }
>
> public double Y
> {
> get { return y; }
> set { y = value; }
> }
> }
>
> class Class1
> {
> static void Method(IPointD point)
> {
> if (point != null)
> {
> point.X = 10;
> point.Y = 10;
> }
> }
>
> [STAThread]
> static void Main(string[] args)
> {
> PointD point = new PointD(1, 1);
> IPointD dPoint = (IPointD)point;
> Method(dPoint);
>
> Console.WriteLine("X = {0}, Y = {1}", point.X, point.Y);
> }
> }
> }
> ---------------------------------------------------------
>
> Why is the call to the method - Method(dPoint) not by reference?
>
> Best regards,
> Paul.


Re: Structure and Interface by Shiva

Shiva
Mon Aug 16 01:19:40 CDT 2004

Yes. IPointD dPoint = (IPointD)point; -> causes boxing (because point is
value type and it is assigned to an interface type), which acutally makes a
copy of point variable on to the managed heap. The copy is then referenced
by dPoint.

"Paul Selormey" <paul@toolscenter.org> wrote in message
news:uZMW7T1gEHA.644@tk2msftngp13.phx.gbl...
Sorry, please first question.
static void Main(string[] args)
{
PointD point = new PointD(1, 1);
IPointD dPoint = (IPointD)point;
Method(dPoint);

Console.WriteLine("X = {0}, Y = {1}", point.X, point.Y);
Console.WriteLine("X = {0}, Y = {1}", dPoint .X, dPoint .Y);
}

The question should be, why is dPoint modified, but point is not?

Does
IPointD dPoint = (IPointD)point;

makes a copy of the point?

Best regards,
Paul.

"Paul Selormey" <paul@toolscenter.org> wrote in message
news:ua2TON1gEHA.3612@TK2MSFTNGP12.phx.gbl...
> Consider the following codes:
> ----------------------------------------------------
> using System;
> namespace ConsoleApplication1
> {
> interface IPointD
> {
> double X {get; set;}
> double Y {get; set;}
> }
>
> struct PointD : IPointD
> {
> private double x;
> private double y;
> public PointD(double x, double y)
> { this.x = x; this.y = y;}
>
> public double X
> {
> get { return x; }
> set { x= value; }
> }
>
> public double Y
> {
> get { return y; }
> set { y = value; }
> }
> }
>
> class Class1
> {
> static void Method(IPointD point)
> {
> if (point != null)
> {
> point.X = 10;
> point.Y = 10;
> }
> }
>
> [STAThread]
> static void Main(string[] args)
> {
> PointD point = new PointD(1, 1);
> IPointD dPoint = (IPointD)point;
> Method(dPoint);
>
> Console.WriteLine("X = {0}, Y = {1}", point.X, point.Y);
> }
> }
> }
> ---------------------------------------------------------
>
> Why is the call to the method - Method(dPoint) not by reference?
>
> Best regards,
> Paul.



Re: Structure and Interface by Paul

Paul
Mon Aug 16 02:48:35 CDT 2004

Thanks for the response and the information.
I suspected boxing, but with the PointD implementing the IPointD,
I assumed system should not be boxing in a cast to its base interface.

The more, I play with struct the more the advantages vanish :-(

Best regards,
Paul.

"Shiva" <shiva_sm@online.excite.com> wrote in message
news:OXC%23%23g1gEHA.3476@tk2msftngp13.phx.gbl...
> Yes. IPointD dPoint = (IPointD)point; -> causes boxing (because point is
> value type and it is assigned to an interface type), which acutally makes
a
> copy of point variable on to the managed heap. The copy is then referenced
> by dPoint.
>
> "Paul Selormey" <paul@toolscenter.org> wrote in message
> news:uZMW7T1gEHA.644@tk2msftngp13.phx.gbl...
> Sorry, please first question.
> static void Main(string[] args)
> {
> PointD point = new PointD(1, 1);
> IPointD dPoint = (IPointD)point;
> Method(dPoint);
>
> Console.WriteLine("X = {0}, Y = {1}", point.X, point.Y);
> Console.WriteLine("X = {0}, Y = {1}", dPoint .X, dPoint .Y);
> }
>
> The question should be, why is dPoint modified, but point is not?
>
> Does
> IPointD dPoint = (IPointD)point;
>
> makes a copy of the point?
>
> Best regards,
> Paul.
>
> "Paul Selormey" <paul@toolscenter.org> wrote in message
> news:ua2TON1gEHA.3612@TK2MSFTNGP12.phx.gbl...
> > Consider the following codes:
> > ----------------------------------------------------
> > using System;
> > namespace ConsoleApplication1
> > {
> > interface IPointD
> > {
> > double X {get; set;}
> > double Y {get; set;}
> > }
> >
> > struct PointD : IPointD
> > {
> > private double x;
> > private double y;
> > public PointD(double x, double y)
> > { this.x = x; this.y = y;}
> >
> > public double X
> > {
> > get { return x; }
> > set { x= value; }
> > }
> >
> > public double Y
> > {
> > get { return y; }
> > set { y = value; }
> > }
> > }
> >
> > class Class1
> > {
> > static void Method(IPointD point)
> > {
> > if (point != null)
> > {
> > point.X = 10;
> > point.Y = 10;
> > }
> > }
> >
> > [STAThread]
> > static void Main(string[] args)
> > {
> > PointD point = new PointD(1, 1);
> > IPointD dPoint = (IPointD)point;
> > Method(dPoint);
> >
> > Console.WriteLine("X = {0}, Y = {1}", point.X, point.Y);
> > }
> > }
> > }
> > ---------------------------------------------------------
> >
> > Why is the call to the method - Method(dPoint) not by reference?
> >
> > Best regards,
> > Paul.
>
>