将引用类型作为方法参数传递 [英] Passing reference type as method parameter

查看:66
本文介绍了将引用类型作为方法参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


A有一个字符串变量(这是一个引用类型)。

现在我定义我的方法:


void MakeFullName(string sNamePrivate)

{

sNamePrivate + =" Gates"

}


主叫代码:

string sName =" Bill";

MakeFullName(sName);


我的变量sName没有改变(相同的Bill)。字符串是一个

引用类型,我希望

它将通过引用传递而不将方法定义为

参数为ref 。只有在将其定义为ref之后我得到了预期的

结果(BillGates)。

void MakeFullName1(ref string sNamePrivate)

{

sNamePrivate + =" Gates"

}


主叫代码:

string sName =" Bill";

MakeFullName1(ref sName);


在下面的文章中,Jesse Liberty明确表示,该参考

参数通过引用传递:
http://www.ondotnet.com/pub/a/dotnet...ps.html?page=2


"值类型传递给方法值(复制品),而

参考类型通过引用有效传递。


这是否意味着,除非我将我的参数声明为 ; REF"我的价值

在mehtod之外永远不会改变?


感谢您的帮助。


Maxim

解决方案

嗨Maxim,


我认为这意味着除非你明确声明否则(通过使用)

" ref"),然后假设

你按价值传递所有参数。


任何人都请更正我,如果我错了。


干杯!


" Maxim" <毫安********* @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

嗨!

有一个字符串变量(这是一个引用类型)。
现在我定义我的方法:

void MakeFullName( string sNamePrivate)
{
sNamePrivate + =" Gates"
}
主叫代码:
string sName =" Bill" ;;
MakeFullName(sName);

我的变量sName没有改变(相同的Bill)。 String是一个
引用类型,我希望
它将通过引用传递,而不将方法
参数定义为ref。只有在将其定义为ref之后我得到了预期的结果(BillGates)。

void MakeFullName1(ref string sNamePrivate)
{
sNamePrivate + =" Gates"
}

调用代码:
string sName =" Bill";
MakeFullName1(ref sName);

在下面的文章Jesse Liberty中明确说,参考
参数通过引用传递:
http://www.ondotnet.com/pub/a/dotnet...ps.html?page=2
"值类型按值传递给方法(复制),而
引用类型通过引用有效传递。

这是否意味着,除非我声明我的参数作为参考我的价值
在mehtod之外永远不会改变?

感谢您的帮助。

Maxim


您好Maxim,

您为测试选择了一个不幸的例子。字符串是一个

引用类型对象,但它具有值类型语义,这意味着字符串是
不可变的。如果你改变一个字符串,即你试图追加,或者更改字符串的任何部分,那么真正发生的是你复制了

整个字符串。


类通常是引用类型,因此在下面的示例中,Person

实例将通过引用自动传递,而不使用ref关键字:


类人物

{

私人字符串_name;


public Person(字符串名称)

{

_name = name;

}


公共字符串名称

{

get

{

return _name;

}

set

{

_name = value;

}

}

}


public static void Main(string [] args)

{

Person p = new Person(" mark");

ChangePersonName(p);


//这将打印新名称

Console.WriteLine(p.Name);

}


public static void ChangePersonName(Person p)

{

p.Name =" new name";

}

在你的例子中我会改变MakeFullName函数返回一个字符串为

结果。


ref关键字是只有当你想传递值类型如

引用时才需要,这在我看来通常是不可取的,因为它使得代码不是可靠的
。你不需要ref关键字通过引用传递普通引用类型

,这将自动发生。


还要注意结构是值类型而不是引用类型:-)


希望有所帮助

Mark R Dawson


" Maxim"写道:

嗨!

A有一个字符串变量(它是一个引用类型)。
现在我定义我的方法:

void MakeFullName(string sNamePrivate)
{
sNamePrivate + =" Gates"
}

调用代码:
string sName =" Bill";
MakeFullName(sName);

我的变量sName没有改变(相同的Bill)。 String是一个
引用类型,我希望
它将通过引用传递,而不将方法
参数定义为ref。只有在将其定义为ref之后我得到了预期的结果(BillGates)。

void MakeFullName1(ref string sNamePrivate)
{
sNamePrivate + =" Gates"
}

调用代码:
string sName =" Bill";
MakeFullName1(ref sName);

在下面的文章Jesse Liberty中明确说,参考
参数通过引用传递:
http://www.ondotnet.com/pub/a/dotnet...ps.html?page=2
"值类型按值传递给方法(复制),而
引用类型通过引用有效传递。

这是否意味着,除非我声明我的参数作为参考我的价值
在mehtod之外永远不会改变?

感谢您的帮助。

Maxim



如果你添加ref关键字到引用类型,实际上你传递了一个

引用。有点像指向C中的指针。

没有引用,当你添加了姓氏时,你把一个新的字符串重新放在

sNamePrivate上,而不是它是。有了它,你

重新分配原始变量。


Hi!

A have a string variable (which is a reference type).
Now I define my Method like that:

void MakeFullName(string sNamePrivate)
{
sNamePrivate+="Gates"
}

The calling code:
string sName="Bill";
MakeFullName(sName);

My variable sName wasn''t changed (the same "Bill"). String is a
reference type and I would expect
that it will be passed by reference without defining the method
parameter as "ref". Only after defining it as "ref" I get the expected
result ("BillGates").
void MakeFullName1(ref string sNamePrivate)
{
sNamePrivate+="Gates"
}

The calling code:
string sName="Bill";
MakeFullName1(ref sName);

In the following article Jesse Liberty explicitly says, that reference
parameters are passed by reference:
http://www.ondotnet.com/pub/a/dotnet...ps.html?page=2

"Value types are passed to methods by value (a copy is made) while
reference types are effectively passed by reference."

Does it mean, that unless I declare my paramter as "ref" my value
outside the mehtod is never changed?

Thanks for you help.

Maxim

解决方案

Hi Maxim,

I think it means that unless you explicitly declare otherwise (by using
"ref"), then assume that
you''re passing all parameters by value.

Anyone please correct me if I am wrong.

Cheers!

"Maxim" <ma*********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

Hi!

A have a string variable (which is a reference type).
Now I define my Method like that:

void MakeFullName(string sNamePrivate)
{
sNamePrivate+="Gates"
}

The calling code:
string sName="Bill";
MakeFullName(sName);

My variable sName wasn''t changed (the same "Bill"). String is a
reference type and I would expect
that it will be passed by reference without defining the method
parameter as "ref". Only after defining it as "ref" I get the expected
result ("BillGates").
void MakeFullName1(ref string sNamePrivate)
{
sNamePrivate+="Gates"
}

The calling code:
string sName="Bill";
MakeFullName1(ref sName);

In the following article Jesse Liberty explicitly says, that reference
parameters are passed by reference:
http://www.ondotnet.com/pub/a/dotnet...ps.html?page=2

"Value types are passed to methods by value (a copy is made) while
reference types are effectively passed by reference."

Does it mean, that unless I declare my paramter as "ref" my value
outside the mehtod is never changed?

Thanks for you help.

Maxim



Hi Maxim,
you chose an unfortunate example for your testing. A string is a
reference type object but it has value type semantics, meaning a string is
immutable. If you change a string i.e you try to append, or change any part
of the string, then what is really happening is that you make a copy of the
entire string.

Classes are usually reference types, so in the example below the Person
instance is automatically passed by reference without the ref keyword:

class Person
{
private string _name;

public Person(string name)
{
_name = name;
}

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}

public static void Main(string[] args)
{
Person p = new Person("mark");
ChangePersonName(p);

//this will print "new name"
Console.WriteLine(p.Name);
}

public static void ChangePersonName(Person p)
{
p.Name = "new name";
}
In your example I would change the MakeFullName function return a string as
the result.

The ref keyword is only required when you want to pass value types like
references, which in my opinion is usually not desirable as it makes code not
as readable. You do not need the ref keyword to pass normal reference types
by reference, this will happen automatically.

Also beware that structs are value types not reference types :-)

Hope that helps
Mark R Dawson

"Maxim" wrote:

Hi!

A have a string variable (which is a reference type).
Now I define my Method like that:

void MakeFullName(string sNamePrivate)
{
sNamePrivate+="Gates"
}

The calling code:
string sName="Bill";
MakeFullName(sName);

My variable sName wasn''t changed (the same "Bill"). String is a
reference type and I would expect
that it will be passed by reference without defining the method
parameter as "ref". Only after defining it as "ref" I get the expected
result ("BillGates").
void MakeFullName1(ref string sNamePrivate)
{
sNamePrivate+="Gates"
}

The calling code:
string sName="Bill";
MakeFullName1(ref sName);

In the following article Jesse Liberty explicitly says, that reference
parameters are passed by reference:
http://www.ondotnet.com/pub/a/dotnet...ps.html?page=2

"Value types are passed to methods by value (a copy is made) while
reference types are effectively passed by reference."

Does it mean, that unless I declare my paramter as "ref" my value
outside the mehtod is never changed?

Thanks for you help.

Maxim



if you add the "ref" keyword to a reference type, you in fact pass a
reference to a reference. kind of like a pointer to a pointer in C.
without the ref, when you added the last name, you reassined
sNamePrivate to a new string, instead of what it was. with it, you
reassigned the original variable.


这篇关于将引用类型作为方法参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆