Re: textbook authors: passing by ref is NOT more efficient!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: How can I pass a multidimensional array as a ref parameter in func
    ... static int ReadFile(ref ushortnArray, ... > Array variable is the pointer to the address. ... > No 'ref' is needed. ... Ref is needed if your variable is not a reference ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: slicing array references
    ... I'm working on some classes with attributes that are array and hash ... 0,1,2 in scalar context is 2. ... there is no need to try variations on how the ref got there. ... similarly, when you just want a single item from an aggregate reference, always use the -> notation. ...
    (perl.beginners)
  • Re: textbook authors: passing by ref is NOT more efficient!
    ... efficiency in terms of performance and we are talking reference objects. ... classic example (using int values, ... Console.WriteLine("Value after by ref routine ", ... reference to the array object even if you don't say "ref", ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: how to pass array and varaible
    ... ref even if you use a direct array. ... Make sure you pass var first, followed by array. ... > they no longer complicate argument passing. ... > a reference can take significantly less time. ...
    (perl.beginners)
  • RE: Passing reference type with ref vs. without
    ... void DoSomething ... ref is used for allowing you to pass value types by reference. ... > Could someone clarify my confusion regarding passing reference types to a ...
    (microsoft.public.dotnet.languages.csharp)