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 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
    }
    }

  • 相关阅读:
    随笔 祝我快乐
    .NET设计模式单件模式(Singleton Pattern)
    随笔 缘分
    随笔 雨季
    数据库设计中的小经验
    一个字节造成的巨大性能差异——SQL Server存储结构
    随笔 淡淡的幸福
    用LINQ还是NHibernate?
    随笔 风筝
    FreeStyle Wishes
  • 原文地址:https://www.cnblogs.com/sumg/p/3742759.html
Copyright © 2011-2022 走看看