标签:
计算机分为5个区
class MainClass { public void Jiaohuan(int x,int y) { x=x+y; y=x-y; x=x-y; } MainClass mainclass = new MainClass(); int a = 12; int b = 35; mainclass.Jiaohuan(a,b); Console.WriteLine ("a={0},b={1}",a,b);//然而实参没有交换 int c =30; int d = c; d = 40; Console.WriteLine ("c={0},d={1}",c,d);
class MainClass { public void Jiaohuan( ref int x,ref int y)//ref只适用于值类型,相当于把值类型转化为引用类型 { x=x+y; y=x-y; x=x-y; } public static void Main (string[] args) { MainClass mainclass = new MainClass(); int a = 12; int b = 35; mainclass.Jiaohuan(ref a,ref b); Console.WriteLine ("a={0},b={1}",a,b); //形参的值发生改变后,实参的值也会发生改变
public void Maopao(int[]a) { for(int i = 0;i<a.Length-1;i++){ for(int j =0;j<a.Length-i-1;j++){ if(a[j]>a[j+1]) { int temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } } int[] s = {1,5,6,7,2,4,3}; int[] h = s; h[1] = 10; Console.WriteLine (s[1]); mainclass.Maopao(s);
int num = 23; object obj = num; Console.WriteLine (obj);
int count = (int)obj; Console.WriteLine (count);
public void PrintArr( params int[] a)//可变数组参数params { foreach(int n in a) { Console.Write (n+" "); } }
//输出数组 int[] sh = {12,54,45,32,45}; mainclass.PrintArr(sh); Console.WriteLine (); mainclass.PrintArr(2,5,7,8,4,9);//使用params后可省去定义数组 Console.WriteLine (); mainclass.PrintArr();
public void PrintArr(int a,int b,params int[] jk)//可变数组参数params与别的值类型同存在 { foreach(int y in jk) { Console.Write (y+" "); } } mainclass.PrintArr(2,5,7,8,4,9);//使用params后可省去定义数组
// keywords_ref.cs
using System;
class App
{
public static void UseRef(ref int i)
{
i += 100;
Console.WriteLine("i = {0}", i);
}
static void Main()
{
int i = 10;
// 查看调用方法之前的值
Console.WriteLine("Before the method calling: i = {0}", i);
UseRef(ref i);
// 查看调用方法之后的值
Console.WriteLine("After the method calling: i = {0}", i);
Console.Read();
}
}
//买烟的方法 +out public int Maiyan(int money,out int change)//把零钱输出来,有out的话是把形参赋值给实参 { int num = money/20; change = money%20; return num; } //输出参数,当返回值为两个,则在方法定义参数处加out, int lingqian; int number = mainclass.Maiyan(50,out lingqian);//拿了多少钱 Console.WriteLine (number); Console.WriteLine (lingqian);
static void Main() { //int i = 10; 改为 int i; // }
1.参数的长度可变。长度可以为0。
2.只能使用一次,而且要放到最后。
// keywords_params.cs using System; class App { public static void UseParams(params object[] list) { for (int i = 0; i < list.Length; i++) { Console.WriteLine(list[i]); } } static void Main() { // 一般做法是先构造一个对象数组,然后将此数组作为方法的参数 object[] arr = new object[3] { 100, ‘a‘, "keywords" }; UseParams(arr); // 而使用了params修饰方法参数后,我们可以直接使用一组对象作为参数 // 当然这组参数需要符合调用的方法对参数的要求 UseParams(100, ‘a‘, "keywords"); Console.Read(); } }
标签:
原文地址:http://www.cnblogs.com/wangsaiws/p/5029458.html