static void Main() { test te=new test();//**改了。 type t=new type("---"); te.gt(t); System.Console.WriteLine("STURCT X = {0}",t.x); cls s = new cls("class.x"); te.gt(s); System.Console.WriteLine("CLASS.s X = {0}",s.x); } private void gt(cls t) { t.p=new cls("class.p.x"); } private void gt(type t) { t.x="struct.x"; }
/// /////////////////////////////////////////////////////////// struct type { public string x; public type(string s) {x=s;} } class cls { public string x; public cls p; public cls(string s) { x=s; } } } C#中的参数是按值传递的。也就是它传递的是一个考贝。。所以不能修改。 改成这样的话。它就是引用传递。 using System;namespace test3 { /// <summary> /// Class1 的摘要说明。 class test {
static void Main() { test te=new test(); type t=new type("---"); te.gt(ref t); System.Console.WriteLine("STURCT X = {0}",t.x); cls s = new cls("class.x"); te.gt(ref s); System.Console.WriteLine("CLASS.s X = {0}",s.x); } private void gt(ref cls t) { t.p=new cls("class.p.x"); t=t.p;//******注意这儿改了。 } private void gt( ref type t) { t.x="struct.x"; }
/// /////////////////////////////////////////////////////////// struct type { public string x; public type(string s) {x=s;} } class cls { public string x; public cls p; public cls(string s) { x=s; } } }} 反正只要你想按引用传递参数的话就得用关健字REF,或是OUT!
{
static void Main()
{
test te=new test();//**改了。
type t=new type("---");
te.gt(t);
System.Console.WriteLine("STURCT X = {0}",t.x); cls s = new cls("class.x");
te.gt(s);
System.Console.WriteLine("CLASS.s X = {0}",s.x); }
private void gt(cls t)
{
t.p=new cls("class.p.x");
} private void gt(type t)
{
t.x="struct.x";
}
/// /////////////////////////////////////////////////////////// struct type
{
public string x;
public type(string s)
{x=s;}
}
class cls
{
public string x;
public cls p; public cls(string s)
{
x=s;
}
}
}
C#中的参数是按值传递的。也就是它传递的是一个考贝。。所以不能修改。
改成这样的话。它就是引用传递。
using System;namespace test3
{
/// <summary>
/// Class1 的摘要说明。
class test
{
static void Main()
{
test te=new test();
type t=new type("---");
te.gt(ref t);
System.Console.WriteLine("STURCT X = {0}",t.x); cls s = new cls("class.x");
te.gt(ref s);
System.Console.WriteLine("CLASS.s X = {0}",s.x); }
private void gt(ref cls t)
{
t.p=new cls("class.p.x");
t=t.p;//******注意这儿改了。
} private void gt( ref type t)
{
t.x="struct.x";
}
/// /////////////////////////////////////////////////////////// struct type
{
public string x;
public type(string s)
{x=s;}
}
class cls
{
public string x;
public cls p; public cls(string s)
{
x=s;
}
}
}}
反正只要你想按引用传递参数的话就得用关健字REF,或是OUT!
静态还是非静态函数没有关系吧,我试过了,结果也一样!to snewxf(心疤):
你也差不多理解我的想法了,我要问的问题是:在不加ref的前提下为什么可以修改类的属性,但就不能修改它自己的引用(指定另一个类)?
http://www.csdn.net/expert/topic/1036/1036250.xml?temp=.3493463