• ref 关键字out关键字


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

    namespace outAndref
    {
    class Program
    {
    static void Main(string[] args)
    {
    }

    //ref修饰方法的参数,在调用的时候必须在变量之前加上ref关键字,只能传递变量,不能传递常量
    //传递的时候 不是传递变量的值,而是传递变量的地址
    //out 也是传递变量的地址,out必须在方法内赋值,ref可以修改其值,也可以不修改
    //out侧重输出,ref侧重修改
    static void TestOut(out int i)
    {
    i = 100;
    }
    static void TestRef(ref int i)
    {
    i++;
    }
    #region 【冒泡排序】
    static void GetMaxAndMin(int[] arr, out int max, out int min)
    {
    //Array.Sort(arr);排序
    for (int i = 0; i < arr.Length - 1; i++)
    {
    for (int j = 0; j < arr.Length - 1 - i; j++)
    {
    if (arr[j] > arr[j + i])
    {
    int temp = arr[j];
    arr[j] = arr[j + i];
    arr[j + 1] = temp;
    }
    }
    }
    max = arr[arr.Length - 1];
    min = arr[0];
    }
    #endregion

    #region[属性]
    public string name;
    public string Name //属性的返回值跟他封装的字段是没有关系的,跟他的get返回的类型有关系,也跟set赋值的类型也有关系
    {
    get {
    return name;
    }
    set
    {
    if (value == "")
    {
    name = "hello world";
    }else
    {
    name=value;
    }
    }
    }

    #endregion
    }
    }

  • 相关阅读:
    Undefined symbols for architecture armv7错误解决方法
    一个iOS 框架介绍:MKNetworkKit
    objective-c之各种数值
    利用openURL,在IOS应用中打开另外一个应用
    hackerrank---List Comprehensions
    LeetCode --- Plus One
    Vim学习与总结
    LeetCode --- Pascal's Triangle II
    LeetCode --- Gray Code
    LeetCode --- Two Sum
  • 原文地址:https://www.cnblogs.com/sumg/p/3742759.html
Copyright © 2020-2023  润新知