473,842 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4576
Gene <go******@intel lireach.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.co m>
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******@intel lireach.com.no spam> wrote in message
news:eW******** ******@TK2MSFTN GP12.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 "Directiona l 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 "Directiona l Attributes" in Visual Studio help or MSDN for more
details.
--------------------
From: "Gene" <go******@intel lireach.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.publi c.dotnet.langua ges.csharp
NNTP-Posting-Host: 65.215.3.2
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1998 74
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
2964
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 following error messages: warning: implicit typename is deprecated, please see the documentation for details Can someone kindly suggest how to get rid of these warnings? The source code follows.
188
17487
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
33
3203
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
34595
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
6520
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
17
2525
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
4
6294
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 world\n'); }
206
8420
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 footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
3
2512
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 == rhs.isbn; } author says the word const modifies the type of implicit "this" paramater.
0
9871
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10944
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10610
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10313
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7855
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5884
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.