473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

return value cannot be ref or out

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

Mar 9 '07 #1
4 1539
Zytan wrote:
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

--
http://barrkel.blogspot.com/
Mar 9 '07 #2
On Mar 9, 2:04 pm, "Zytan" <zytanlith...@yahoo.comwrote:
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?

Mar 10 '07 #3
"Zytan" <zy**********@yahoo.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
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.

Mar 10 '07 #4
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

Mar 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Murat Tasan | last post by:
so i have a situation that i know cannot be solved directly, and i've already worked around it, but i was hoping to find an explanation as to why this behavior is as it is... i have an abstract...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
11
by: MLH | last post by:
The following procedures found at http://ffdba.com/downloads/Send_Mail_With_Outlook_Express.htm are meant to work together in harmony to effect eMail sends via OE. The last procedure (FN SplitB)...
2
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I can successfully insert and update the oracle database by calling a oracles stored proc from my .net code. This oracle stored proc is returning some value. I cannot see that...
6
by: =?Utf-8?B?Um9i?= | last post by:
Hi, If I call my own DLL from Javascript, a HRESULT is returned. Javascript cannot cast this to an integer. Is there any other way of typecasting this returnvalue to integer, wihout using the...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.