Re: about the class string
- From: Saad Rehmani <saad.rehmani@xxxxxxxxx>
- Date: Wed, 7 Jun 2006 21:41:50 +0000 (UTC)
Tony,
A 'ref' parameter has an extra layer of indirection that allows you to change the actual reference.
Do a bit of reading up on what happens when you pass a variable by ref and see if you can figure out why this won't compile:
public void foo(ref string s) { }
foo(ref "inline-string");.
but this will:
public void foo(string s) { }
foo("inline-string");.
--
Saad Rehmani / Prodika / Dallas / TX / USA
Hello!!
I have a simple question and that is about the class Class1 below.
As I have learned the datatype string a reference type.
I set the number field to "1" in main and pass this number to method
foo.
In foo I changed number to "2" but when I come back in main it's still
"1".
If I use ref then number is changed also in main but as I mentioned
before
because
string is a reference type I shouldn't have to use ref.
Can somebody explain why number is not "2" in main.
class Class1
{
public void foo(string number)
{
number = "2";
}
[STAThread]
public static void Main(string[] args)
{
string number = "1";
new Class1().foo(number);
}
}
//Tony
.
- References:
- about the class string
- From: Tony Johansson
- about the class string
- Prev by Date: Re: Socket connect vs. bind? What is the difference
- Next by Date: Re: OO Design Debate
- Previous by thread: Re: about the class string
- Next by thread: Re: about the class string
- Index(es):