C#中函数参数的形参和实参即加ref 或加 out 与全部不加

来源:互联网 发布:华为海军 知乎 编辑:程序博客网 时间:2024/05/12 05:29

在VB中有形参和实参这两个概念,我不知道在C#中有没有。

但是在参数前加ref out 或不加,其效果和VB中的形参和实参一样。

一个实例搞定一切

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            int i=1;            int j = 1;            Console.WriteLine("执行前   i={0}   j={1}", i, j);            Sum1(ref i,j);            Console.WriteLine("执行后   i={0}   j={1}", i, j);            Console.WriteLine("--------------------");            int k = 1;            int l = 1;            Console.WriteLine("执行前   k={0}   l={1}", k, l);            Sum2(out k, l);            Console.WriteLine("执行后   k={0}   l={1}", k, l);            Console.WriteLine("--------------------");                        Console.ReadKey();        }                static void  Sum1(ref int i,  int j)        {            i++;                    }        static void Sum2( out int k, int l)        {            k = 6;//必须要有一个明确的赋值运算,且要在k变量在使用前赋值        }    }}