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

  • Thread starter Thread starter Gene
  • Start date Start date
G

Gene

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
 
Gene said:
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.
 
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
 
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" <[email protected] 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: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 65.215.3.2
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.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 exactly
am 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
 
Back
Top