首页 > 代码库 > out传值

out传值

class Program
{
public void jia(int a, int b)
{
a = a + b;
Console.WriteLine(a);
}

}

 

static void Main(string[] args)
{
Console.Write("请输入a=");
int a = int.Parse(Console.ReadLine());
Console.Write("请输入b=");
int b = int.Parse(Console.ReadLine());
Program hanshu = new Program();

// hanshu.jia(a,b);
//形参 传值
//只是将值给它,并不是把变量名(箱子)也给它

Console.WriteLine(a);//这个a跟函数里的a是不一样的
Console.ReadLine();

}

 

 

 

 

class Program
{

public void jia1(int a,out int b)
{
b = a + 5;
}

}

static void Main(string[] args)
{
Console.Write("请输入a=");
int a = int.Parse(Console.ReadLine());
Console.Write("请输入b=");
int b = int.Parse(Console.ReadLine());
Program hanshu = new Program();
hanshu.jia1(a,out b);
Console.WriteLine(b);
//out传值时,仅仅是将变量名称(箱子)传给函数
//在进行完成循环体之后,将这个变量以及里面的值一起拿出来


//实参 传址
//将变量名(箱子)给它

 Console.ReadLine();

}

 

列题:

class Program      {         public void  hou()          {              int sum = 0;              for (int i = 10; i > 0; i--)              {                 if (i == 10)                  {                      sum = 1;                  }                  else                  {                     sum = (sum + 1) * 2;                  }              }              Console.Write(sum);          }            public int jiafen(int  a)          {              a+= 10;              return a;          }            public void jiafen2(int[] a)          {              int l= a.Length;              for (int i = 0; i < l; i++)             {                  a[i] += 10;              }          }            public int [] jiafen4(int[] a)          {              int l = a.Length;              for (int i = 0; i < l; i++)              {                  a[i] += 10;              }              return a;          }            public void  jiafen3(int[] a, out int[] b)          {              int l = a.Length;              for (int i = 0; i < l; i++)              {                  a  [i] = a[i] + 10;              }              b = a;          }            static void Main(string[] args)          {              //out 传值    形式参数:只给值,不给变量名(传值)    实际参数:将变量名传给函数(传址)              //out是实参                Program hanshu=new Program();              //猴子              //hanshu.hou();              //Console.ReadLine();                //输入班级人数,根据人数输入每个人的成绩             //本班都是少数民族学生,每个人+10分              //写一个专门+10分的函数,参数是这个分数的数组              Console.WriteLine("请输入班级的人数");              int renshu = int.Parse(Console.ReadLine());              int[] fen = new int[renshu];              for (int i = 0; i < renshu ;i++ )             {                  Console.WriteLine("请输入第{0}名同学的成绩",(i+1));                  fen[i] = int.Parse(Console.ReadLine());              }              //for(int i=0;i <renshu ;i++)              //{              //    fen[i ]= hanshu.jiafen(fen[i]);              //}                //hanshu.jiafen2(fen );              //hanshu.jiafen3(fen,out chengji);                int []chengji=new int [renshu ];              hanshu.jiafen3(fen,out chengji );              foreach(int aa in chengji  )              {                  Console.WriteLine(aa );              }             foreach (int aa in fen )              {                 Console.WriteLine(aa);             }             Console.ReadLine();          }     } }

 

out传值