472,358 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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
3 4473
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
2
by: nick | last post by:
the following is my programming code and compile message why the warning message arise, have i done somethings wrong? #include<stdio.h> typedef struct card{ int abc; }card;
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
4
by: arnuld | last post by:
i am learning C and doing the exercise 1-1 of K&R2, where K&R ask to remove some parts of programme and experiment with error, so here i go: #include <stdio.h> int main () { printf('hello...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
3
by: arnuld | last post by:
this is from C++ Primer 4/e page 260: there is a class named Sales_item and same_isbn is a member function of that class. bool same_isbn(const sales_item &rhs) const { return isbn ==...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.