string parameters

  • Thread starter Thread starter A.M
  • Start date Start date
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
 
Value

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

--
Jared Parsons [MSFT]
(e-mail address removed)
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 said:
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)
 
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/default.asp?url=/library/en-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.
 
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
 
GajananK said:
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)
 
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.
 
Hi Ali,

Does our reply make sense to you?

If you still have anything unclear, please feel free to post, we will help
you.

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.
 
Back
Top