Re: textbook authors: passing by ref is NOT more efficient!
- From: "Ian Semmel" <anyone@xxxxxxxxxxxxxxxxx>
- Date: Tue, 1 Apr 2008 20:07:45 +0000
I reckon you should tell your students to get on with the job at hand instead of persuing the anal-retaining pastime of trying to out-guess the C# compiler.
People who engage in this form of "efficiency" usually end up with highly inefficient programs as they are concentrating on areas which have no bearing on the application they are trying to develop. Ask them how long it takes to write a screen compared to the time difference in using 'ref' or not.
"Aaron Watters" <aaron.watters@xxxxxxxxx> wrote in message news:6dca001b-aae6-4cfa-bfde-48d7a9604ebf@xxxxxxxxxxxxxxxxxxxxxxxxxxx:
Hi. I've been teaching C# and I'm tired of students
telling me that they passed ints by ref when they don't
need to because it's "more efficient". I've seen this myth
repeated in many textbooks, but it's completely wrong
in C# (and it's always wrong for ints in any language).
In the case of C++ if you are passing arrays (if I recall)
it can be pretty important to pass by ref and not by
value because otherwise the whole (3GB) array gets
copied onto the call stack.
However, in the case of C# all arrays
are reference types, so you always actually copy a
reference to the array object even if you don't say "ref",
so there is no imperative to pass by ref unless you want
to change the binding of the variable naming the array.
For all common value types the size of a reference to the
value is about the same size as the size of the value itself,
and you are adding an additional level of indirection
-- so you are not saving anything in space or time by passing
by ref when you don't have to.
The only exception is "structs" which can hypothetically
be large, but they are only used for special purposes
and should be avoided in most vanilla programming.
Please correct me if I'm wrong, or if not, please
*stop* *confusing* *my* *students* who should always
pass by value unless it's absolutely necessary to pass
by ref.
Annoyed: -- Aaron (Stagnant) Watters
===
http://www.xfeedme.com/nucular/gut.py/go?FREETEXT=immoral+english
.
- References:
- textbook authors: passing by ref is NOT more efficient!
- From: Aaron Watters
- textbook authors: passing by ref is NOT more efficient!
- Prev by Date: Re: Delegate Equal() returning false when it shouldn't?
- Next by Date: simple way of continuing out of outter loop
- Previous by thread: Re: textbook authors: passing by ref is NOT more efficient!
- Next by thread: Re: textbook authors: passing by ref is NOT more efficient!
- Index(es):
Relevant Pages
|