design question : struct or class

P

peter x

Enjoyed the article - thanks for pointing it out :) Peter


L# said:
application. I've never seen or heard anything that would make me believe
that the size of a value type determines whether they reside on the stack or
on the heap.

Mmm, indeed, I thought I read somewhere that the framework checks
this, but apparently it doesn't, because using large structs has a
negative effect in perfomance.

[Source:
http://msdn.microsoft.com/library/d...-us/dv_vstechart/html/vbtchUseClassStruct.asp]

We learn, every day.
 
J

Jon Skeet [C# MVP]

Being objects, they are passed by value; meaning that the pointer to
the object is passed by value.

Passing an object doesn't happen in .NET. Only passing a reference
happens - and as you say, it's passed by value by default in C#.
Eventually it has the same effect as
being passed by reference.

No it doesn't.
Placing the ref keyword in front of it, won't make a difference.

Yes it will. Here's a sample program:

using System;

public class Test
{
static void Main()
{
string x = "hello";
PassByValue (x);
Console.WriteLine (x);
PassByRef (ref x);
Console.WriteLine (x);
}

static void PassByValue (string y)
{
y = "there";
}

static void PassByRef (ref string y)
{
y = "there";
}
}

PassByValue and PassByRef are identical apart from the way in which
their parameters are passed - yet they have completely different
effects.
 
J

Jon Skeet [C# MVP]

But doesn't String behave completely different than other Objects?

No. What makes you say it does?

If you want an example which doesn't use strings at all, here it is:

using System;

public class Test
{
int number;

public int Number
{
get { return number; }
}

Test (int number)
{
this.number = number;
}

static void Main()
{
Test t = new Test(10);
PassByValue(t);
Console.WriteLine (t.Number);
PassByRef(ref t);
Console.WriteLine (t.Number);
}

static void PassByValue (Test t)
{
t = new Test(20);
}

static void PassByRef (ref Test t)
{
t = new Test(20);
}
}
 
L

L#

No. What makes you say it does?

If you want an example which doesn't use strings at all, here it is:

So if you modify the object, there's no difference in passing by
value/passing by reference; but if you create a new object, there's a
difference. Didn't know that!
 
J

Jon Skeet [C# MVP]

So if you modify the object, there's no difference in passing by
value/passing by reference; but if you create a new object, there's a
difference. Didn't know that!

The important difference is that modifying the object a parameter's
value refers to doesn't actually change the value of the parameter.
Changing the value of the parameter (e.g. by creating a new object, but
that's only one type of new value) is the important thing, and that's
where the difference between pass-by-reference and pass-by-value
semantics lies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top