473,473 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

about the class string

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


Jun 7 '06 #1
2 1212
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

Jun 7 '06 #2
Tony Johansson <jo*****************@telia.com> wrote:
I have a simple question and that is about the class Class1 below.
As I have learned the datatype string a reference type.


See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 8 '06 #3

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
4
by: Andrew | last post by:
Is it possible a constructor of a class to use a funtion defined in the same class ????
19
by: JustSomeGuy | last post by:
I have a class that has a static member variable. string x; x should never change during use and should be intialized to "abcd". How does one do this?
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
6
by: wASP | last post by:
Hello everyone, I'm new to C# and ASP.NET, so pardon my stupidity on this one. I'm having a problem with referencing methods/functions external to a class member function. My code is as...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
1
by: UJ | last post by:
I am doing development on a machine and everything was working fine. The name of the project was ECS to I made all my references as ~/ECS/... Worked great. Put it on the final server running...
5
by: raj | last post by:
I know what interfaces are and what they used for etc. Today i am learning about serilization. I know to mark the class "serializable" and implement ISerializable interface, and then implement...
4
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. The question is about this method GetCows() {...} which is a member in the generic...
0
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...
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.