How should this sentence be interpreted

T

Tony Johansson

Hi!

You need to create a simple class or structure that contains only value
types.
You must create the class or structure so that it runs efficiently as
possible. You
must be able to pass the class or structure to a procedure without concern
that the
procedure will modify it.

Which of the following should you use ?
A reference class
or
A value structure

I don't fully understand what the sentence mean by saying "without concern
that the
procedure will modify it." Does they mean that the original passed argument
must not be changed or does it mean
that it doesn't matter if the procedure change the passed argument ?

My choice between a reference class and a value structure is the following.
If the original passed argument is not allowed to be changed we must choose
a struct.
If it doesn't matter if the passed argument is changed in the procedure I
would go for a class because then
the passing mechanism is more efficient because of passing only the refence.


//Tony
 
A

Arne Vajhøj

Hi!

You need to create a simple class or structure that contains only value
types.
You must create the class or structure so that it runs efficiently as
possible. You
must be able to pass the class or structure to a procedure without concern
that the
procedure will modify it.

Which of the following should you use ?
A reference class
or
A value structure

I don't fully understand what the sentence mean by saying "without concern
that the
procedure will modify it." Does they mean that the original passed argument
must not be changed or does it mean
that it doesn't matter if the procedure change the passed argument ?

Good question. Ask those that created the question.

The first sound more likely.
My choice between a reference class and a value structure is the following.
If the original passed argument is not allowed to be changed we must choose
a struct.

There are immutable classes. String is a wellknown example.
If it doesn't matter if the passed argument is changed in the procedure I
would go for a class because then
the passing mechanism is more efficient because of passing only the refence.

You can pass a value type by reference.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
Good question. Ask those that created the question.

The first sound more likely.


There are immutable classes. String is a wellknown example.


You can pass a value type by reference.

Arne

The correct answer accoring to the book is a value structure
For me again it all depende on what they mean with this part of the sentence
"without concern that the procedure will modify it." I hate unclear
question that is hard to really understand what they mean.

//Tony
 
J

Jesse Houwing

* Tony Johansson wrote, On 31-1-2010 20:53:
Hi!

You need to create a simple class or structure that contains only value
types.
You must create the class or structure so that it runs efficiently as
possible. You
must be able to pass the class or structure to a procedure without concern
that the
procedure will modify it.

Which of the following should you use ?
A reference class
or
A value structure

I don't fully understand what the sentence mean by saying "without concern
that the
procedure will modify it." Does they mean that the original passed argument
must not be changed or does it mean
that it doesn't matter if the procedure change the passed argument ?

My choice between a reference class and a value structure is the following.
If the original passed argument is not allowed to be changed we must choose
a struct.
If it doesn't matter if the passed argument is changed in the procedure I
would go for a class because then
the passing mechanism is more efficient because of passing only the refence.


//Tony

Well,

If the struct is passed (by value by default) to a procedure, no changes
made to that value, will propagate back to the calling method, as it has
it's own version.

Were it passed by reference, then any changes made to it in the called
procedure would be reflected in the version (the same version, because
it was passed by reference) held by the calling method.

So that would be in favor of a value type/struct.

The point about efficiency is really a moot point in my opinion.

And the fact that it only holds value types makes it a suitable
candidate for a structure.
 
T

Tony Johansson

Jesse Houwing said:
* Tony Johansson wrote, On 31-1-2010 20:53:

Well,

If the struct is passed (by value by default) to a procedure, no changes
made to that value, will propagate back to the calling method, as it has
it's own version.

Were it passed by reference, then any changes made to it in the called
procedure would be reflected in the version (the same version, because it
was passed by reference) held by the calling method.

So that would be in favor of a value type/struct.

The point about efficiency is really a moot point in my opinion.

And the fact that it only holds value types makes it a suitable candidate
for a structure.

Do you agree that the question is very unclear ?

//Tony
 
P

Peter Duniho

Tony said:

Hello again. :)
You need to create a simple class or structure that contains only value
types.

Irrelevant to the question, IMHO. It's a red herring.
You must create the class or structure so that it runs efficiently as
possible.

Also irrelevant. Reference types and value types have their own
performance characteristics, and using one where you need the other can
always be inefficient, regardless of which one you're talking about.
You
must be able to pass the class or structure to a procedure without concern
that the procedure will modify it.

Frankly, this comes the closest to being relevant. But since you can
make a reference type immutable, and since it's a REALLY bad idea to
make a value type mutable, it doesn't really say much either.

Also, passing a value type by reference allows the method to modify the
original instance anyway.
Which of the following should you use ?
A reference class
or
A value structure

I don't fully understand what the sentence mean by saying "without concern
that the
procedure will modify it." Does they mean that the original passed argument
must not be changed or does it mean
that it doesn't matter if the procedure change the passed argument ?

Hopefully the former. The latter is controlled entirely by whether you
pass by value or pass by reference (i.e. with "ref" or "out" to pass by
reference).

But since you can design a class that ensures it's not modifiable, this
doesn't really give the answer either.
My choice between a reference class and a value structure is the following.
If the original passed argument is not allowed to be changed we must choose
a struct.

How does that solve this example:

struct Test
{
public Test(int i)
{
Value = i;
}

public int Value { get; private set; }
}

void MethodA()
{
Test test = new Test(3);

MethodB(ref test);

Console.WriteLine("value: " + test.Value.ToString());
}

void MethodB(ref Test test)
{
test = new Test(5);
}

Hint: it doesn't.

On the other hand, if we ignore the possibility of passing by reference,
and look only at passing by value, how does this _not_ address the same
requirement:

class Test
{
public Test(int i)
{
Value = i;
}

public int Value { get; private set; }
}

void MethodA()
{
Test test = new Test(3);

MethodB(test);

Console.WriteLine("value: " + test.Value.ToString());
}

void MethodB(Test test)
{
test.Value = 5; // compiler error here!
}

Hint: it's exactly the same. The called method cannot change the passed
argument, because the class itself is immutable (that is, it can't be
changed after initialization).
If it doesn't matter if the passed argument is changed in the procedure I
would go for a class because then
the passing mechanism is more efficient because of passing only the refence.

That's a very good point, and it's what I'm referring to when I point
out that classes and structs each have their own performance
characteristics.

Specifically, if the struct is small enough, the fact that it's always
being copied isn't really a problem. But, if you've got a type that
contains 100 bytes or so of data, making that a struct can be a big
performance problem. If you need it to not be modifiable, you don't
have to use a struct. You just need an immutable class.

Bottom line: the book you're using is not very good. Get a new one. I
recommend Jon Skeet's C# In Depth.

Pete
 
P

Peter Duniho

Tony said:
Do you agree that the question is very unclear ?

It's not just unclear. It's an awful, misleading question. It pretends
you can make design decisions based on factors that have nothing to do
with the design decision it's asking you about.

I certainly see where the author is coming from, trying to emphasize
that when passed by value, changes to a value type in a method cannot
propagate back to the caller, because the value type is passed as a
copy, not the original.

But that's not true when passed by reference, and the implication that
you can't also accomplish the same with reference types is also false.
The illustration misses the mark in both directions!

(Note: I am assuming here no code in the method that does dangerous
things like using "unsafe" to convert to a pointer, or using reflection
to access private members directly. Since intentionally dangerous code
like that could also modify the caller's own copy of a value type, I
consider that a perfectly reasonable assumption. If you assume someone
bypassing the language features protecting the data, NO data is safe no
matter what you do, not even if it's a value type passed by value).

Pete
 
A

Arne Vajhøj

The correct answer accoring to the book is a value structure
For me again it all depende on what they mean with this part of the sentence
"without concern that the procedure will modify it." I hate unclear
question that is hard to really understand what they mean.

Do you have the luxury of choosing book yourself?

If yes, then I will recommend that you consider an
alternative book.

Arne
 
A

Alberto Poblacion

Tony Johansson said:
You need to create a simple class or structure that contains only value
types.
You must create the class or structure so that it runs efficiently as
possible. You
must be able to pass the class or structure to a procedure without concern
that the
procedure will modify it.

Which of the following should you use ?
A reference class
or
A value structure

It looks like you are preparing for a certification exam. Ideally, all
the questions in the exam should be perfectly clear and unambiguous, and
have a single undisputable answer. Unfortunately, you will find that quite
often this is not the case. When this happens, if you wish to pass the exam,
you need to start thinking about what it is that the question writer had in
mind when the question was written, and what it is that they are expecting
you to answer, regardless of whether they are 100% right.

In the case of this particular question, it is most likely that the
"suspect" sentence was meant for you to think about the fact that the
structure will be passed by value by default, and therefore the changes made
to its members in the procedure would not be propagated to the caller. As
others have pointed out, this need not always be the case, but anyway the
question writers expected you to think in that way, and therefore you are
expected to mark the second answer (a structure).
 
J

Jesse Houwing

* Tony Johansson wrote, On 31-1-2010 23:11:
Do you agree that the question is very unclear ?

//Tony

I totally agree. I also recognize the question from the 70-536 exam
preps. I had to coach quite a few colleagues for this exam, and I have
my doubts about a lot of the questions in this exam.

It reminds me of my C++ exam at the university which came down to
unraveling someones pointer mess and solving pointer puzzles that had
lots of *'s, =>'s and ()'n in them. I've never properly learned C++, but
I've become a master in refactoring crappy code and reading the intent
of other programmers.
 
G

Göran Andersson

Tony said:
The correct answer accoring to the book is a value structure

The correct answer according to me is a class.

A structure is more complicated to implement correctly, so stick to use
classes unless there is a good reason to use a structure. There is
nothing in the question that gives any real reason to use a structure.

Something that would give a reason to use a structure would sound more
like this:

"Construct a type that will represent a single entity, consisting of a
small number of components that are all value types."
For me again it all depende on what they mean with this part of the sentence
"without concern that the procedure will modify it." I hate unclear
question that is hard to really understand what they mean.

I think that it's reasonably clear what that means, but it's certainly
nothing that excludes using a class.

The class should of course be immutable to meat that requirement, but a
structure should also be immutable so there is nothing extraordinary
about that. Using the fact that structures are passed by value to
protect the value from change is only misuse of the value type semantics.
 
P

Peter Duniho

Göran Andersson said:
[...]
A structure is more complicated to implement correctly, so stick to use
classes unless there is a good reason to use a structure. There is
nothing in the question that gives any real reason to use a structure.

I certainly agree with that. The entire question is poorly constructed,
given the answer they expect to be given.
Something that would give a reason to use a structure would sound more
like this:

"Construct a type that will represent a single entity, consisting of a
small number of components that are all value types."

How does that specifically argue in favor of a struct?

IMHO, the most important distinguishing factor between a struct and
class is the specific value type semantics that the struct offers. The
composition of the struct is merely a performance question, which may or
may not be important. But the semantics of the type is always present
and affects everything about how the type is used.

Thus, it seems to me that the only truly unambiguous statement that
would lead to the conclusion to use a struct would be something like
"construct a type that when assigned or passed, employs copy semantics
rather than reference semantics".
I think that it's reasonably clear what that means, but it's certainly
nothing that excludes using a class.

The class should of course be immutable to meat that requirement, but a
structure should also be immutable so there is nothing extraordinary
about that. Using the fact that structures are passed by value to
protect the value from change is only misuse of the value type semantics.

Reference types are passed by value too. Unless of course "ref" or
"out" is used, but that applies to value types as well.

I do agree that the modification or lack thereof of an object when
passed as an argument to a method is not a good way to choose between a
value type or reference type. But let's not get confused about "passing
by value/by reference" in the process of discussing value types and
reference types. :)

Pete
 

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