473,406 Members | 2,404 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,406 software developers and data experts.

string parameters

A.M
Hi,

Are string parameters in functions by refrence or by value ?

Thanks,
Ali
Nov 15 '05 #1
8 2186
It's by value unless the specify the param is by ref

----- A.M wrote: ----

Hi

Are string parameters in functions by refrence or by value

Thanks
Al

Nov 15 '05 #2
Value

Parameters are passed in .NET by value unless you use a ref, or out in the
signature.

--
Jared Parsons [MSFT]
ja******@online.microsoft.com
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"

"A.M" <IH*******@sapm123.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi,

Are string parameters in functions by refrence or by value ?

Thanks,
Ali

Nov 15 '05 #3
A.M wrote:
Are string parameters in functions by refrence or by value ?


Unless marked otherwise, all parameters are by value. However, this does not
work the way it did in VB6 and below. The value is a duplicate reference to
an object, not a duplicate object.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #4
Hi Ali,

Thank you for posting in the community!

Based on my understanding, you want to know if the string class is pass to
a method by value or by reference.

==============================
Just as the community stated, everything passed to a method is default by
value, unless it is marked by "ref" keyword, then it will by reference.

I want to point out that pass by value or by reference type has nothing to
do with the reference type or value type. That is, for reference type, it
also has 2 types: by value and by reference. Please see the 2 samples below:

1). Passing Reference Types by Value
using System;
class PassingRefByVal
{
static void Change(int[] arr)
{
arr[0]=888; // This change affects the original element.
arr = new int[5] {-3, -1, -2, -3, -4}; // This change is local.
Console.WriteLine("Inside the method, the first element is: {0}",
arr[0]);
}

public static void Main()
{
int[] myArray = {1,4,5};
Console.WriteLine("Inside Main, before calling the method, the first
element is: {0}", myArray [0]);
Change(myArray);
Console.WriteLine("Inside Main, after calling the method, the first
element is: {0}", myArray [0]);
}
}

Output
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888

2). Passing Reference Types by Reference
using System;
class PassingRefByRef
{
static void Change(ref int[] arr)
{
// Both of the following changes will affect the original variables:
arr[0]=888;
arr = new int[5] {-3, -1, -2, -3, -4};
Console.WriteLine("Inside the method, the first element is: {0}",
arr[0]);
}

public static void Main()
{
int[] myArray = {1,4,5};
Console.WriteLine("Inside Main, before calling the method, the first
element is: {0}", myArray [0]);
Change(ref myArray);
Console.WriteLine("Inside Main, after calling the method, the first
element is: {0}", myArray [0]);
}
}

Output
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3

~~~~~~~~~~~~
In the sample you will see that: for reference type by value, the
reference's content was not copied, but the reference variable self is
copied. So you can change the original reference variable's content, but
you can not change the reference variable itself.

So for string class without "ref" keyword, you "may" change the string
instance's content, but you can not change the string instance self.(That
is point to another string)
But, string class is a special class, whose content can not be modified. So
you can not do this:

string str="jeffrey";
f(str);

private void f(string str1)
{
str1[0]='a'; //this line will generate compile error
}

Hope I have clarified for you :-) For more information, please refer to:
"Passing Parameters"
http://msdn.microsoft.com/library/de...us/csref/html/
vclrfPassingMethodParameters.asp

===================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5
Also make sure you are talking about string datatype and not about the class. If you use String class then it is pass by ref, if you use string datatype then by valu

----- A.M wrote: ----

Hi

Are string parameters in functions by refrence or by value

Thanks
Al

Nov 15 '05 #6
GajananK wrote:
Also make sure you are talking about string datatype and not about
the class. If you use String class then it is pass by ref, if you use
string datatype then by value


string is an alias for System.String.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #7

Hi,

In C#, string datatype is a one by one relation with System.String class,
so it is also a reference type. They are all default pass by value, for
more details, please refer to my reply in this post.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #8
A.M wrote:
Hi,

Are string parameters in functions by refrence or by value ?

Thanks,
Ali


Here's a _great_ article on parameter passing, which you should
definitely read: http://www.yoda.arachsys.com/csharp/parameters.html
Nov 15 '05 #9

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

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
4
by: Niyazi | last post by:
Hi I am trying to insert some value into SQL Server 2000 tables and I am keep getting the "Input string was not in a correct format" error. When user fills the Form it updates the table call...
2
by: roopeman | last post by:
i need your help, my code as below : //----------------------------------------------------------------------- //Wrote by Michael April 30 2005...
3
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b...
1
by: Caroline | last post by:
I am trying to update a record though a stored procedure and parameters, but I keep getting this error: Additional information: Argument 'Prompt' cannot be converted to type 'String'. Any...
6
by: Manuel Canas | last post by:
Hello there, this is the piece of code that if giving problems right now; cnSQL = New SqlConnection(ConnectionString) **** strSQL = "UPDATE tb_product SET (ProductID = @cProductID, Code =...
8
by: **Developer** | last post by:
Been reading some of the Marshal doc and am a little confused. Anyway, I've been using the following and it works OK. Public Declare Auto Function SHGetFolderPath Lib "shell32.dll" (ByVal hWnd...
16
by: Mark A. Sam | last post by:
Hello, I am having a problem with imputting into a string variable: Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany, txtPhone, txtEmail, txtComment, chkGrower,...
3
by: =?Utf-8?B?SmFuIEhlcHBlbg==?= | last post by:
Hi, I've a question. I'm developing a windows application and for the application i need to run functions and procedures that are stored in a database. Here is an example that i tried to get...
15
by: cj | last post by:
I've got a long string to be written to a field in a sql db table via stored procedure. For some reason I find only 258 chars are being written. Any ideas? relevant bits of my VB2005 code: ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.