Structure and Interface

  • Thread starter Thread starter Paul Selormey
  • Start date Start date
P

Paul Selormey

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.
 
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.
 
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.

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.
 
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 said:
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.

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 said:
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.
 
Back
Top