By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

byte array as an argument has "implicit" ref behavior?

P: n/a
I'm not sure I understand this behavior (at least in the context of C#).

I understand the use of "ref" and "out" keywords as they apply to method
arguments, and then something this morning stopped me in my tracks. I must
have used FileStream.Read a thousand times and it never dawned on me that I
never have to specify either an "out" or "ref" when I pass the byte array
that I pass to this function- yet it returns the bytes read from the file.

The documentation (msdn) further confused me because they show an "in"
keyword as part of the method definition... how does "in" fit into this?

[C#]
public override int Read(
in byte[] array,
int offset,
int count
);

In C++ arrays and structures are "passed" by pointer- is this what C# is
doing for me? I haven't defined a struct and tried this either- what exactly
am I looking at?

Thanks in advance,
Gene


Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Gene <go******@intellireach.com.no> wrote:
I'm not sure I understand this behavior (at least in the context of C#).

I understand the use of "ref" and "out" keywords as they apply to method
arguments, and then something this morning stopped me in my tracks. I must
have used FileStream.Read a thousand times and it never dawned on me that I
never have to specify either an "out" or "ref" when I pass the byte array
that I pass to this function- yet it returns the bytes read from the file.

The documentation (msdn) further confused me because they show an "in"
keyword as part of the method definition... how does "in" fit into this?
Not sure about this, as I get a different behaviour with my MSDN - it
shows it as [In, Out] which doesn't help much either. I think it's best
just to know that it's a parameter which is a reference passed by value
(see below).
[C#]
public override int Read(
in byte[] array,
int offset,
int count
);

In C++ arrays and structures are "passed" by pointer- is this what C# is
doing for me? I haven't defined a struct and tried this either- what exactly
am I looking at?


Arrays are reference types in .NET - in other words, the value you pass
in is a reference to an array. (This reference is passed by value.)

Modifications made to the array within the method are therefore seen
after the method has exited - just like with (say) StringBuilder or
ArrayList.

See http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html for more information
about this kind of thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
100
Hi Gene,
In .NET arrays are reference types so the Read can fill out your array
without using the ref or out modifier. What you see in MSDN is that the
buffer argument has *In* and *Out* attributes assigned to it. This is used
for ComInterop and P\Invoke only and has nothing to do with the way Read
method threads the argument. The argument is passed by value.

HTH
B\rgds
100

"Gene" <go******@intellireach.com.no spam> wrote in message
news:eW**************@TK2MSFTNGP12.phx.gbl...
I'm not sure I understand this behavior (at least in the context of C#).

I understand the use of "ref" and "out" keywords as they apply to method
arguments, and then something this morning stopped me in my tracks. I must
have used FileStream.Read a thousand times and it never dawned on me that I never have to specify either an "out" or "ref" when I pass the byte array
that I pass to this function- yet it returns the bytes read from the file.

The documentation (msdn) further confused me because they show an "in"
keyword as part of the method definition... how does "in" fit into this?

[C#]
public override int Read(
in byte[] array,
int offset,
int count
);

In C++ arrays and structures are "passed" by pointer- is this what C# is
doing for me? I haven't defined a struct and tried this either- what exactly am I looking at?

Thanks in advance,
Gene

Nov 15 '05 #3

P: n/a
What you see is called "Directional Attributes"

FileStream.Read([In, Out] byte[]....)

If you observe the [In, Out] is an attribute that has been applied to a
parameter. And as a user you don't have to explicitly mention this when you
use the API.

Directional attributes are optional. Some languages provide keywords that
enable you to modify the directional flow of method parameters. So the
"out" & "ref" you see in C# are just keywords in that lanugage.

Search for "Directional Attributes" in Visual Studio help or MSDN for more
details.
--------------------
From: "Gene" <go******@intellireach.com.no spam>
Subject: byte array as an argument has "implicit" ref behavior?
Date: Mon, 17 Nov 2003 10:39:00 -0500
Lines: 28
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <eW**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 65.215.3.2
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:199874
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I'm not sure I understand this behavior (at least in the context of C#).

I understand the use of "ref" and "out" keywords as they apply to method
arguments, and then something this morning stopped me in my tracks. I must
have used FileStream.Read a thousand times and it never dawned on me that I
never have to specify either an "out" or "ref" when I pass the byte array
that I pass to this function- yet it returns the bytes read from the file.

The documentation (msdn) further confused me because they show an "in"
keyword as part of the method definition... how does "in" fit into this?

[C#]
public override int Read(
in byte[] array,
int offset,
int count
);

In C++ arrays and structures are "passed" by pointer- is this what C# is
doing for me? I haven't defined a struct and tried this either- what exactlyam I looking at?

Thanks in advance,
Gene


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.