473,385 Members | 1,372 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,385 software developers and data experts.

String variable as parameter!

public void Foo(string one)
{
one = "two";
}
main()
{
string one = "one";
Foo(one);
Console.writeline(one);
}

What is return result? String variable 'one' is "one" or "two"?
thanks!

--Tony

Aug 25 '06 #1
2 1382
As it stands, "one" will be written to the console.

(warning: slightly confusing terminology ahead; read slowly...)
normally for reference types (such as the string class) the *reference* to
the object is passed *by value*; by *reassigning* (one = "two"), you break
this relationship. Note that if instead you had the below you would see
"two", since we haven't *reassigned* the variable (we have just changed
properties of the referenced item):
class SomeClass {
public string Text;
}
public void Foo(SomeClass one)
{
one.Text = "two";
}
main()
{
SomeClass one = new SomeClass();
one.Text = "one";
Foo(one);
Console.writeline(one.Text);
}
===
equally, if we used "ref string one" as the parameter (and called via
"Foo(ref one)") then our reassignment would be reflected in our calling
code.

Marc
Aug 25 '06 #2
Hi,

Dont you think you would have get a faster , 100% correct answer by just
running it?
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<Gi*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
public void Foo(string one)
{
one = "two";
}
main()
{
string one = "one";
Foo(one);
Console.writeline(one);
}

What is return result? String variable 'one' is "one" or "two"?
thanks!

--Tony

Aug 25 '06 #3

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...
5
by: Aamer Nazir | last post by:
Hi, I am having problems setting the value of a variable in a SQL String that I have to create dynamically in my procedure. The code that I currently have is as follows: set...
5
by: Nasos Makriyiannis | last post by:
Hi, I'm new to XSL and I was wondering if there is a string-compare function available. I am using the following IF statement but it does not seem to be working: <xsl:if...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
2
by: Mark D'Souza via .NET 247 | last post by:
In trying to create a Panel object with the FindControl methodusing a string variable as the paramater. The code is below. The variable 'testvariable' is equal to pnl_0 which is anexisting Panel on...
21
by: M D | last post by:
You know how you assume you know until you find out you don't know. Well, I typed into a function definition "..., new String("")). I know what I want. Everyone reading this knows what I want....
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
4
by: Chris Forone | last post by:
hello group, why have i to bracket the second ctor param in the following example? thx & hand, chris #include <fstream> #include <iterator> int main()
1
by: Doogster | last post by:
Hi folks This is a bit of a long shot, but does anyone know how to pass a date variable into an Oralce SQL string in VBA. Here's an example of the string: SQLCommand.CommandText = "SELECT...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.