Re: Reference Types and Value Types

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: daniel (user_at_example.com)
Date: 09/02/04


Date: Thu, 02 Sep 2004 16:57:29 -0500

Ahh, thank you Lateralus for the quick reply. I have a quick follow up
question, though:

If a reference type is always passed by reference, then (while stupid
and messy):

<code>
// with a return type this time!
void func1()
{
        SomeType myType = new myType();
        func2(myType);
}
void func2(SomeType myType)
{
        func3(mytype);
}
void func3(SomeType myType)
{
        Console.Write(myType.ToString());
}
</code>

will not cause a substantial amount of increased overhead at all (other
than variables on the stack), because only one object is every created.

Thanks,
-daniel

Lateralus [MCAD] wrote:

> daniel,
> To my knowledge you can't pass a "reference" type by value if you wanted
> to. It is automatically passed by reference. If you have a value type and
> you want to maintain it's value across method calls, you need to pass it by
> reference.
>
> example should print out 12:
>
> private void Test()
> {
> int index = 0;
> Test2(ref index);
> Console.Write(index.ToString());
> }
>
> private void Test2(ref index)
> {
> index = 12;
> }
>



Relevant Pages

  • Re: back once again...
    ... reference to type ... a, signed char ... int dycObjectP(dyt obj); ... void dycBeginClass; ...
    (comp.lang.misc)
  • [PATCH 12/15] dm: add exports
    ... Export core device-mapper functions for manipulating mapped devices ... void dm_get ... struct dm_target; ... * Drop the reference with dm_put when you finish with the object. ...
    (Linux-Kernel)
  • Re: Strong thread safety and lock free?
    ... you definitely can pack pointer and counter into single word. ... No, there is no emulation of locks, just 2 reference counters ... Automatic alignment but typically no explicit alignment required ... static void debuglog(const char* fmt, ...
    (comp.programming.threads)
  • Re: Whats your favorite language and why? (LINUX)
    ... efficient distributed intra-process message-passing algorithm on several ... architectures that incurs virtually zero-overheads. ... by reference without having to do anything at all. ... void producer_threads{ ...
    (comp.programming)
  • Re: C to Java migration - how to cope with passing by value only?
    ... > void Foo(Color cl, JButton bn) { ... In C++, with reference parameters, I suspect you're not really doing quite ... public JButton button; ...
    (comp.lang.java.programmer)