zoukankan      html  css  js  c++  java
  • 2014.8.30.ref,out,params,enum,递归

    (一)ref

    函数形参变量的输入有两种方式:传值,传址。而ref则为传址。eg:

     1 static int Add(ref int n)
     2 {
     3     Console.WriteLine("Add----1:{0}",n);
     4     n = n + 10;
     5     Console.WriteLine("Add----2:{0}",n);
     6     return n;
     7 }
     8 static void Main(string[] args)
     9 {
    10     int a = 5;
    11     Console.WriteLine("Main----1:{0}",a);
    12     Add(ref a);//执行函数Add后,并没有把a的值更新,但加上ref则把a在内存中的地址也传给函数了,相应的a指向的地址也就随之改变了。a = Add(a);
    13     Console.WriteLine("Main----2:{0}",a);
    14     Console.ReadKey();
    15 }

    打印结果为:

    (二)out

    out顾名思义,即输出。相当于一个函数可以有多个返回值,这是C#中特有的。

     1 static int Add(int n,out int x)
     2 {
     3     Console.WriteLine("Add----1:{0}",n);
     4     n = n + 10;
     5     x = 33;
     6     Console.WriteLine("Add----2:{0}",n);
     7     return n;
     8 }
     9 static void Main(string[] args)
    10 {
    11     int a = 5;
    12     int x;
    13     Console.WriteLine("Main----1:{0}",a);
    14     a=Add(a,out x);
    15     Console.WriteLine("Main----2:{0},{1}",a,x);//x相当于第二个返回值,它的值也改变了,至少目前我是这么理解的
    16     Console.ReadKey();
    17 }

    打印结果为:

    (三)params

    在数组形参前面使用,可以赋多个值。eg:

     1 static void PaiXu(params int[] nums)
     2 {
     3     for (int i = 1; i < nums.Length - 1; i++)
     4     {
     5 for (int j = 1; j <= nums.Length - i; j++)
     6 {
     7     if (nums[j] > nums[j - 1])
     8     {
     9 int temp = nums[j];
    10 nums[j] = nums[j - 1];
    11 nums[j - 1] = temp;
    12     }
    13 }
    14     }
    15     for (int i = 0; i < nums.Length; i++)
    16     {
    17 Console.Write(nums[i]);
    18     }
    19 }
    20 
    21 PaiXu(3, 5, 7, 2, 1, 8);//调用的时候可以赋任意多个值了

    (四)enum 枚举

    枚举是由程序员定义的类型,与类或结构一样。

     *与结构一样,枚举是值类型,因此直接存储它们的数据,而不是分开存储成引用和数据。
    *枚举只有一种类型的成员:命名的整数值常量。
    eg:
     1 enum days
     2 {
     3     Sun = 1,
     4     Mon,
     5     Tue,
     6     Wed,
     7     Tur,
     8     Fri=15,
     9     Sat
    10 }
    1 Console.WriteLine((int)days.Mon);
    2 Console.WriteLine((int)days.Sat);

    打印结果为:

  • 相关阅读:
    Stacks And Queues
    Programming Assignment 5: Burrows–Wheeler Data Compression
    Data Compression
    Regular Expressions
    Programming Assignment 4: Boggle
    Oracle 查询表的索引包含的字段
    pycharm
    Java文件:追加内容到文本文件
    okhttp 使用response.body().string()获取到的数据是一堆乱码
    彻底解决unable to find valid certification path to requested target
  • 原文地址:https://www.cnblogs.com/zsmj001/p/3948438.html
Copyright © 2011-2022 走看看