有关C#引用类型变量的疑问。
我刚刚接触到C#其中对其引用型的变量有一些疑问,希望各位不吝赐教!
C#中的变量如(string,int)都是类的实例,而实例作为函数参数时是引用型的参数。因此在函数体内可以修改外部的实例。但为什么当一个string,int等变量作为参数的时候却不是引用型的参数,而是外部数据的一个拷贝。如
class test
{
public string a;
}
class Program
{
public static void f(System.String s)
{
s = "HELLO!";
}
public static void f1(test t)
{
t.a = "hello!";
}
static void Main(string[] args)
{
System.String ss = "123";
f(ss);
Console.WriteLine(ss);
test t = new test();
f1(t);
Console.WriteLine(t.a);
}
}
结果显示的是123和hello!,既然string和test都是类,本质上是相同的,为什么会有这样的差别?
问题点数:20、回复次数:18Top
1 楼dayasky(.Neting)回复于 2006-03-04 15:33:38 得分 5
因为string是特殊的引用类型,而int是值类型
在你通过对string赋值的时候,会产生并返回一个新的string实例
//我从C# primer plus书里看来的,而且工作中证明确实是这么回事Top
2 楼Macosx(结贴)回复于 2006-03-04 15:44:26 得分 5
Main中ss和f()中的s是两个不同的对象 string虽然是一个class 但用起来像一个struct
f(ss)传给f()的是"123"而不是ss 很有意思的问题Top
3 楼sysenter(有容乃大,无欲则刚)回复于 2006-03-04 16:11:02 得分 0
谢谢 dayasky,Macosx
我一般理解 "特殊的**" 是不太习惯了,呵呵,总想找一种通用的方式来理解。我一直是把string作为一个和其他类一样非常普通的类来理解的,看来现在行不通了。Top
4 楼sysenter(有容乃大,无欲则刚)回复于 2006-03-04 16:23:09 得分 0
int既然是一个值类型,为什么也有像ToString()这样的方法呢?Top
5 楼Macosx(结贴)回复于 2006-03-04 16:28:39 得分 0
值类型不是不能有方法啊 你理解错了Top
6 楼zengkun100(夜的孩子)回复于 2006-03-04 16:41:50 得分 0
去看看《.NET 框架程序设计修订版》吧,讲得很清楚得Top
7 楼xiaomaolover(小M)回复于 2006-03-04 16:53:50 得分 5
因为 f1(test t)
传入的是test 是一个类
在内存中test只是一个指针。指向的是一块数据区。
Console.WriteLine(t.a);
这时的t.a已经是被凼数处理过的的了。所以
t.a = "hello!";
至于第一个123
虽然system.stirng是一个类。但是一种数据类型。所以只是值传递..
Top
8 楼xiaomaolover(小M)回复于 2006-03-04 16:55:26 得分 0
zengkun100(夜的孩子) 说的对。。
NET 框架程序设计这书里有写Top
9 楼MonkWang(象写情书一样写程序)回复于 2006-03-04 16:59:08 得分 0
恩 string 的确是一个特殊的引用类型!Top
10 楼Neu_Theone(编程苦行僧)回复于 2006-03-04 17:06:28 得分 5
看下面的代码对你的理解也有帮助:
string s1 = "123";
string s2 = s1;
s2 = "456";
Console.WriteLine(s1);
控制台输出的是123
实际上string之间的赋值是另开辟了一块内存将源string拷贝到目标string,对于长字符串这是很耗内存的。Top
11 楼sysenter(有容乃大,无欲则刚)回复于 2006-03-04 17:07:17 得分 0
不是可以这样用吗?
int a;
string b = a.ToString();Top
12 楼Neu_Theone(编程苦行僧)回复于 2006-03-04 17:11:10 得分 0
值类型的toString()方法,是一个装箱的概念,调用该方法的时候已经是Object类型的数据了。
不知道说的对不对,大家姑且一听Top
13 楼sysenter(有容乃大,无欲则刚)回复于 2006-03-04 17:34:39 得分 0
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemobjectclasstostringtopic.asp
根据上面所说的,tostring应该是一个类的方法:
This method returns a human-readable string that is culture-sensitive. For example, for an instance of the Double class whose value is zero, the implementation of Double.ToString might return "0.00" or "0,00" depending on the current UI culture.
The default implementation returns the fully qualified name of the type of the Object.
Notes to Implementers: This method can be overridden in a derived class to return values that are meaningful for that type. For example, the base data types, such as Int32, implement ToString so that it returns the string form of the value that the object represents. Derived classes that require more control over the formatting of strings than ToString provides must implement IFormattable, whose ToString method uses the current thread's CurrentCulture property.
Top
14 楼mib23()(☆☆☆☆☆)回复于 2006-03-05 14:46:57 得分 0
不错,有点启发!Top
15 楼Markc(家穷人丑,治安靠狗,交通靠走,通讯靠吼,发财靠抢)回复于 2006-03-05 16:10:25 得分 0
tostring()是把目标对象转化为又意义的字符串。比如美举的某个项 name=2。。。Top
16 楼sysenter(有容乃大,无欲则刚)回复于 2006-03-10 11:06:00 得分 0
----------------------------------------------------------------------------
看下面的代码对你的理解也有帮助:
string s1 = "123";
string s2 = s1;
s2 = "456";
Console.WriteLine(s1);
控制台输出的是123
实际上string之间的赋值是另开辟了一块内存将源string拷贝到目标string,对于长字符串这是很耗内存的。
-------------------------------------------------------------------------------
但对于普通的对象就不是这样了:
class test
{
public string a;
}
static void Main(string[] args)
{
test t1 = new test();
test t2 = new test();
t2 = t1;
t2.a = "qqq";
Console.WriteLine(t1.a);
}
结果就是qqqTop
17 楼mbh0210(独孤求败)回复于 2006-03-10 11:23:47 得分 0
string是一个特殊的引用类型,描述不清楚。。。。在使用的时候感觉和值类型一样的。。。Top
18 楼yuanarea(Sail before)回复于 2006-03-10 12:03:38 得分 0
mark~Top




