zoukankan      html  css  js  c++  java
  • ref与out的区别(C#)

    1. 使用ref型参数时,传入的参数必须先被初始化。而out,必须在方法中对其完成初始化;

     1     static void Method(out int i)
    2 {
    3 i = 23;
    4 }
    5 static void Main()
    6 {
    7 int value;
    8 Method(out value);
    9 // 现在是 23
    10 }

    2. 使用ref和out时,在方法的参数和执行方法时,都要加ref或out关键字;

    3. ref用在需要被调用者的引用的时候,而out适合用在需要retrun多个返回值的地方;

     1      static void Main()
    2 {
    3 int value;
    4 string str1, str2;
    5 Method(out value, out str1, out str2);
    6 // value 现在为 23
    7 // str1 现在为 "我被返回"
    8 // str2 还是 null;
    9 }

    10 static void Method(out int i, out string s1, out string s2)
    11 {
    12 i = 23;
    13 s1 = "我被返回";
    14 s2 = null;
    15 }
    16
    17

    4. 使用Ref的时候必须要先赋值,而out并不需要赋值。

    就像是网上总结的那样:ref是有进有出,out是只出不进。

  • 相关阅读:
    c++链表实现学生成绩管理系统(简易版)
    IOS动画讲解
    栈的实现
    Masonry的使用
    二叉树详解-2
    二叉树详解-1
    CoreData的使用-2
    NSPredicate 详解
    CoreData的使用-1
    IOS常用手势用法
  • 原文地址:https://www.cnblogs.com/assur/p/2221278.html
Copyright © 2011-2022 走看看