惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - XiaoBei

【Oracle经典】132个oracle热门精品资料——下载目录 ORACLE日期时间函数大全 Oracle 11g 软件及补丁下载地址 - XiaoBei Oracle 10G软件及补丁下载地址 - XiaoBei HP大中华区总裁孙振耀退休十五天后九大感言 TSM简介(二) - 策略、介质与服务器管理 TSM简介(一)- 原理与特点 - XiaoBei 【转】物以类聚,人以群分--走出软件作坊:三五个人十来条枪 如何成为开发正规军(十一) - XiaoBei 【转】将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十) 探求数据仓库关键环节ETL的本质 .Net开源的CMS、Portal系统大全 说服别人的六种好方法 BI前端展示工具评估 学习.net应该知道什么 几十个源代码网站 - XiaoBei 【转】实施费用也能DIY--走出软件作坊:三五个人十来条枪 如何成为开发正规军(九) 【转】水清则无鱼--走出软件作坊:三五个人十来条枪 如何成为开发正规军(八) ADO.NET常用对象详解之:Command对象 ASP.Net中MD5和SHA1加密的几种方法
参数修饰符ref,out ,params的区别
XiaoBei · 2008-07-10 · via 博客园 - XiaoBei

C#中有三个关键字-ref,out ,params,可是这三个之间的区别你都明白了吗?

那么我们就来认识一下参数修饰符ref,out ,params吧,还有它们的区别。

第一: params
一个可以让方法(函数)的拥有可变参数的关键字。

  原则:在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。

  示例(拷贝到vs2005中即可用,下面不再说明)

public partial class Form1 : Form
    {
        
public static void UseParams(params int[] list)
        {
            
string temp = "";
            
for (int i = 0; i < list.Length; i++)
                temp 
= temp +" " +list[i].ToString();
            MessageBox.Show(temp);
        }
public static void UseParams2(params object[] list)
        {
            
string temp = "";
            
for (int i = 0; i < list.Length; i++)
                temp 
= temp + " " + list[i].ToString();
            MessageBox.Show(temp);
        } 
public Form1()
        {
 InitializeComponent();
        }
private void button1_Click(object sender, EventArgs e)
        {
            UseParams(
123);//看参数是3个
            UseParams(12);   //看参数是2个,可变吧
 

              UseParams2(

1'a'"test");int[] myarray = new int[3] ...{ 101112 };
            UseParams(myarray); 
//看也可以是容器类,可变吧:)
        }
}

第二:  out
这是一个引用传递L。
原则一:当一个方法(函数)在使用out作为参数时,在方法中(函数)对out参数所做的任何更改都将反映在该变量中。
原则二:当希望方法返回多个值时,声明 out 方法非常有用。使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。
原则三:若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。
原则四:不必初始化作为 out 参数传递的变量,因为out 参数在进入方法(函数)时后清空自己,使自己变成一个干净的参数,也因为这个原因必须在方法返回之前为 out 参数赋值(只有地址没有值的参数是不能被.net接受的)。
原则五:属性不是变量,不能作为 out 参数传递。
原则六:如果两个方法的声明仅在 out 的使用方面不同,则会发生重载。不过,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的:
class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(out int i) {i = 10;}
}
而以下重载声明是无效的:
class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有关传递数组的信息,请参见使用 ref 和 out 传递数组。
示例附后

第三:  ref
ref仅仅是一个地址!!!
原则一:当一个方法(函数)在使用ref作为参数时,在方法中(函数)对ref参数所做的任何更改都将反映在该变量中。
原则二:调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
原则三:若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值可以被传递到 ref 参数。
原则四:ref参数传递的变量必须初始化,因为ref参数在进入方法(函数)时后还是它自己,它这个地址指向的还是原来的值,也因为这个原因ref参数也可以在使用它的方法内部不操作。
原则六:如果两种方法的声明仅在它们对 ref 的使用方面不同,则将出现重载。但是,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的:
class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
但以下重载声明是无效的:
class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有关传递数组的信息,请参见使用 ref 和 out 传递数组。
示例

    public static string TestOut(out string i)
        {
            i 
= "out b";
            
return "return value";
        }
public static void TestRef(ref string i)
        {
            
//改变参数
            i = "ref b";
        }
public static void TestNoRef(string refi)
        {
            
// 不用改变任何东西,这个太明显了
            refi = "on c";
        } 
public Form1()
        {
            InitializeComponent();
        }
private void button1_Click(object sender, EventArgs e)
        {
            
string outi;//不需要初始化
            MessageBox.Show(TestOut(out outi));//返回值
              
//输出"return value";
            MessageBox.Show(outi);//调用后的out参数
              
//输出"out b";
 string refi = "a"// 必须初始化
            TestRef(ref refi); // 调用参数
            MessageBox.Show(refi);
             
//输出"ref b";
            TestNoRef(refi);//不使用ref
            MessageBox.Show(refi);
             
//输出"ref b";
        }