什么是ref和out?如何在asp.net中使用? [英] What is ref and out and how to use in asp.net ?
本文介绍了什么是ref和out?如何在asp.net中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是ref和out,以及如何在asp.net中使用?
请举例说明.
What is ref and out and how to use in asp.net ?
Please explain with an example .
推荐答案
ref
和out
控制如何将数据传递给您的方法.
通常,参数按值传递-变量的副本被复制并传递.因此,您对方法中的变量所做的任何更改都将被丢弃.
ref
andout
control how data is passed through to your methods.
Normally, a parameter is passed by value - a copy of the variable is made and is passed through. So any changes you make to teh variable inside your method are discarded.
{
string s = "hello";
MyMethod(s);
Console.WriteLine(s);
}
void MyMethod(string str)
{
str = "goodbye";
}
打印"hello",因为复制了"s"变量.
ref
更改了-传递的是对"s"的引用:
Prints "hello", because the "s" variable was copied.
ref
changes that - what is passed through is a reference to "s":
{
string s = "hello";
MyMethod(ref s);
Console.WriteLine(s);
}
void MyMethod(ref string str)
{
str = "goodbye";
}
打印再见",因为该方法现在可以影响外部变量.
out
的功能大致相同,但是MyMethod不能使用"s"变量的外部值-它只能设置外部值:
Prints "goodbye" because the method can now affect the outer variable.
out
does much the same thing, but the external value of the "s" varaible is not available to MyMethod - it can just set the external value:
{
string s;
MyMethod(out s);
Console.WriteLine(s);
}
void MyMethod(out string str)
{
str = "goodbye";
}
这篇关于什么是ref和out?如何在asp.net中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文