Redian新闻
>
请教 pass by value or reference 的问题
avatar
请教 pass by value or reference 的问题# DotNet - 窗口里的风景
o*a
1
今天学pass by value or ref.
有点疑问, 如果说string 在c#里面是reference type的话,为什么我下面的例子中m
is still cate , but not mouse.
static void Main(string[] args)
{
string m = "cat";
string n = "dog";
SwapString(m, n);
Console.WriteLine("m is " + m);//m is still cat
Console.WriteLine("n is " + n);//n is still dog
Point pnt1 = new Point(2,3);
Point pnt2 = new Point(20,30);
SwapObject(pnt1,pnt2);
Console.WriteLine("X: " + pnt1.x + " Y:" + pnt1.y); //x is 5, y
is 6
Console.WriteLine("X: " + pnt2.x + " Y: " + pnt2.y); //x is 20,
y is 30
}
static void SwapString(string s1,string s2)
{
s1 = "mouse";
string temp;
temp = s1;
s1 = s2;
s2 = temp;
}
static void SwapObject(Point arg1, Point arg2)
{
arg1.x = 5;
arg1.y = 6;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
class Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
avatar
o*a
2
想起来了 string 里面的= 被override了

m

【在 o**********a 的大作中提到】
: 今天学pass by value or ref.
: 有点疑问, 如果说string 在c#里面是reference type的话,为什么我下面的例子中m
: is still cate , but not mouse.
: static void Main(string[] args)
: {
: string m = "cat";
: string n = "dog";
: SwapString(m, n);
: Console.WriteLine("m is " + m);//m is still cat
: Console.WriteLine("n is " + n);//n is still dog

avatar
k*n
4
string是特殊的reference type,
其行为是value type的

【在 o**********a 的大作中提到】
: 想起来了 string 里面的= 被override了
:
: m

avatar
o*a
5
今天学pass by value or ref.
有点疑问, 如果说string 在c#里面是reference type的话,为什么我下面的例子中m
is still cate , but not mouse.
static void Main(string[] args)
{
string m = "cat";
string n = "dog";
SwapString(m, n);
Console.WriteLine("m is " + m);//m is still cat
Console.WriteLine("n is " + n);//n is still dog
Point pnt1 = new Point(2,3);
Point pnt2 = new Point(20,30);
SwapObject(pnt1,pnt2);
Console.WriteLine("X: " + pnt1.x + " Y:" + pnt1.y); //x is 5, y
is 6
Console.WriteLine("X: " + pnt2.x + " Y: " + pnt2.y); //x is 20,
y is 30
}
static void SwapString(string s1,string s2)
{
s1 = "mouse";
string temp;
temp = s1;
s1 = s2;
s2 = temp;
}
static void SwapObject(Point arg1, Point arg2)
{
arg1.x = 5;
arg1.y = 6;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
class Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
avatar
o*a
6
想起来了 string 里面的= 被override了

m

【在 o**********a 的大作中提到】
: 今天学pass by value or ref.
: 有点疑问, 如果说string 在c#里面是reference type的话,为什么我下面的例子中m
: is still cate , but not mouse.
: static void Main(string[] args)
: {
: string m = "cat";
: string n = "dog";
: SwapString(m, n);
: Console.WriteLine("m is " + m);//m is still cat
: Console.WriteLine("n is " + n);//n is still dog

avatar
k*n
8
string是特殊的reference type,
其行为是value type的

【在 o**********a 的大作中提到】
: 想起来了 string 里面的= 被override了
:
: m

avatar
n*f
9
pass by value
比如, x=2 然后你 pass x into 任何 function/sub, x 本身的值, 不变;
pass by ref
比如, x=2 然后你 pass x into 任何 function/sub, x 本身的值, 有可能会被改
变--要看具体的 code;

m

【在 o**********a 的大作中提到】
: 今天学pass by value or ref.
: 有点疑问, 如果说string 在c#里面是reference type的话,为什么我下面的例子中m
: is still cate , but not mouse.
: static void Main(string[] args)
: {
: string m = "cat";
: string n = "dog";
: SwapString(m, n);
: Console.WriteLine("m is " + m);//m is still cat
: Console.WriteLine("n is " + n);//n is still dog

avatar
d*m
10
String is immutable reference type, meaning that new string object will
always be created. even though the assignment may make you think it trys to
modify the content of string.
So if you do:
sring a= "cat";
sring b=a;
a="dog";
a new "dog" object is created and assigned to a, and b is still pointing to
"cat".

m

【在 o**********a 的大作中提到】
: 今天学pass by value or ref.
: 有点疑问, 如果说string 在c#里面是reference type的话,为什么我下面的例子中m
: is still cate , but not mouse.
: static void Main(string[] args)
: {
: string m = "cat";
: string n = "dog";
: SwapString(m, n);
: Console.WriteLine("m is " + m);//m is still cat
: Console.WriteLine("n is " + n);//n is still dog

avatar
d*m
11
String is immutable reference type, meaning that new string object will
always be created. even though the assignment may make you think it trys to
modify the content of string.
So if you do:
sring a= "cat";
sring b=a;
a="dog";
a new "dog" object is created and assigned to a, and b is still pointing to
"cat".
And pass by reference actually does a copy of the reference, similar to what
the following line does:(in c# there is no such concept of pointer to
pointer)
sring b=a;

m

【在 o**********a 的大作中提到】
: 今天学pass by value or ref.
: 有点疑问, 如果说string 在c#里面是reference type的话,为什么我下面的例子中m
: is still cate , but not mouse.
: static void Main(string[] args)
: {
: string m = "cat";
: string n = "dog";
: SwapString(m, n);
: Console.WriteLine("m is " + m);//m is still cat
: Console.WriteLine("n is " + n);//n is still dog

avatar
W*n
12
string就是一大坑,it's immutable
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。