zoukankan      html  css  js  c++  java
  • ref 与 out传递参数

    
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    
    

    namespace ConsoleApplication3
    {
    class Program
    {
    static void Main(string[] args)
    {
     
    RefOut值类型按引用传递 p4 = new RefOut值类型按引用传递();
    int a = 1, b = 2;
    p4.Swap(ref a,ref b);
    int s;
    p4.Total(out s);
    Console.WriteLine("调换的顺序为{0},{1}",a,b );
    Console.WriteLine("Total的值为{0}",s);

    Console.ReadKey();
    }
    }

    
    

    public int AddParams(params int[] number)
    {
    int sum = 0;
    foreach (int item in number)
    {
    sum += item;
    }
    return sum;
    }
    }
    public class Person3
    {
    private int age;
    public int Age
    {
    get { return age; }
    set { age = value; }
    }
    public int this[int a]
    {
    get { return age; }
    set { age = value; }
    }

    
    


    }

    
    

    public class RefOut值类型按引用传递
    {
    public void Swap(ref int a, ref int b)
    {
    int t;
    t = b;
    b = a;
    a = t;
    }
    public void Total(out int s)
    {
    s = 1000;
    }

    
    

    }
    }

     

     ref : 谁调用,谁负责

    out: 输出参数,传参数到方法之前,可以先不赋值。在方法内部,必须要有给参数赋值的语句

  • 相关阅读:
    2.15 STL复习
    20190214Test(栈与队列)
    STL列表链式前向星
    链式前向星(邻接表)
    Priority_queue详解
    List详解
    NOIP2019计划
    第二章笔记
    第一章笔记
    本地文件上传GitHub
  • 原文地址:https://www.cnblogs.com/dark_acme/p/5268964.html
Copyright © 2011-2022 走看看