return value cannot be ref or out

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

It seems I cannot return a reference to a value type as the return
value of a method. I will have to use an out parameter instead. Is
there another way to do it?

Zytan
 
Zytan said:
It seems I cannot return a reference to a value type as the return
value of a method. I will have to use an out parameter instead. Is
there another way to do it?

No, it's not possible to return a reference to a value type. This is
part of the reason value types are useful - they prevent aliasing. If
you want to return a reference to a value type, make it a reference type
instead. Value types are seldom useful, except for things which need
have value semantics, and thus would usually be immutable - and
therefore returning a reference would be semantically pointless.

-- Barry
 
It seems I cannot return a reference to a value type as the return
value of a method. I will have to use an out parameter instead. Is
there another way to do it?

Nope. That would be the equivalent of taking a pointer to a value (in C
++ terms), which is one of the things that C# stops you from doing
(because of the kind of mayhem one can cause that way). No pointers
into the stack, no pointers into the midst of objects: them's the
rules!

As Barry said, one solution is to create a reference type and copy the
value into it, and return a reference to that. Of course, the correct
solution depends upon context... what (in the larger sense) are you
trying to accomplish?
 
Zytan said:
It seems I cannot return a reference to a value type as the return
value of a method. I will have to use an out parameter instead. Is
there another way to do it?

Zytan


Not from C#, two languages I know off, ILAsm and C++/CLI, allows you to return a reference
to a value type, as long as it's not in a stack location.
Here's a sample using ILAsm as a producer and CPP as consumer, other managed languages
cannot party.

// ILAsm file: refret.il
..assembly extern mscorlib {}
..assembly refret {}

..class public Test extends [mscorlib]System.Object {
.field static int32 s_x
.method public static int32& f() {
ldsflda int32 Test::s_x
ret
}
}

Save as refret.il , and
compile with: ilasm /out:refret.dll /dll refret.il

//C++/CLI consumer
#using "refret.dll"
int main()
{
// call a method that returns a ref to an int.
int% ret = Test::Foo();
...

}
save as : userefret.cpp
and compile with
cl /clr:safe userefret.cpp

Willy.
 
Thanks everyone for replying. I was kind of hoping Google didn't get
either of my posts, because I realized afterwards that if C# allowed a
reference to a value type it would totally break down what C# is all
about. This hit me when I realized that I wouldn't even be able to
use it as I wanted to, anyway, in my C# program, because C# just isn't
built around that style of coding, and it would've been a worse
design.

Zytan
 

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

Back
Top