C#源代码—值类型参数演示,引用类型参数演示
2016-01-13 11:26
405 查看
值类型参数演示
引用类型参数演示
引用类型参数演示
using System; class Swaper { public void Swap(int x,int y) //被调用方,其中x和y是整型形参 { int temp; temp = x; x = y; y = temp; Console.WriteLine("交换之后:{0},{1}", x, y); } } class TestMethod { static void Main() //调用方,其中a和b是整型实参 { Swaper s = new Swaper(); //创建对象 Console.WriteLine("请任意输入两个整型数:"); int a = Convert.ToInt32(Console.ReadLine()); int b = Convert.ToInt32(Console.ReadLine()); s.Swap(a,b); //调用并传递参数 Console.WriteLine("交换之前:{0},{1}", a, b); } }
引用类型参数演示
using System; class Swaper { public void Swap(ref int x,ref int y) //被调用方,其中x和y是引用型形参 { Console.WriteLine("形参的值未交换:{0},{1}", x, y); int temp; temp = x; x = y; y = temp; Console.WriteLine("形参的值已交换:{0},{1}", x, y); } } class TestMethod { static void Main() //调用方,其中实参是a和b的引用 { Swaper s = new Swaper(); //创建对象 Console.WriteLine("请任意输入两个整型数:"); int a = Convert.ToInt32(Console.ReadLine()); int b = Convert.ToInt32(Console.ReadLine()); s.Swap(ref a,ref b); //调用并传递参数 Console.WriteLine("实参的值已交换:{0},{1}", a, b); } }
引用类型参数演示
using System; class Analyzer { //从文件路径中分离路径和文件名,定义了两个输出参数 public void SplitPath(string path,out string dir,out string filename) { int i; //寻找路径和文件名之间的间隔符\或:所在位置 for (i = path.Length; i >=0; i--) { char c = path[i - 1]; if (c == '\\' || c == ':') break; } //提取路径和文件名 dir = path.Substring(0, i-1); //0为起始位置,i-1为子字符串长度 filename = path.Substring(i);//从i取,如String a = “abc”;a.substring(1) 为bc } } class TestMethod { static void Main() //调用方,其中实参dir和file是输出参数 { Analyzer a = new Analyzer(); //创建对象 Console.WriteLine(“请输入一个文件的路径:"); string path = Console.ReadLine(); string dir, file; //没有赋初值 a.SplitPath(path, out dir, out file); //调用方法 Console.WriteLine(“文件所在目录:{0}\n文件名:{1}", dir, file); } }
相关文章推荐
- C#对七牛云的操作
- C#圆半径圆的周长,使用嵌套类型计算圆面上点到圆心的距离
- C#源代码—统计从键盘输入一行字符英文字母的个数
- C#源代码—在windows窗体中添加一个名为lblShow的Lable控件4,创建可变字符串,字符串转换为字符型
- C#源代码—从数组x复制到数组y,将数组x的元素排序
- C#源代码—声明委托,通过委托对象调用所指向的方法
- C#源代码—在windows窗体中添加窗体控件,btnOK按钮的Click事件
- C#源代码—在windows窗体中添加一个名为lblShow的Lable控件3
- C# ListView 自定义ToolTip 显示
- C#源代码—在windows窗体中添加一个名为lblShow的Lable控件2
- C#删除只读文件或文件夹(解决File.Delete无法删除文件)
- C#源代码—在windows窗体中添加一个名为lblShow的Lable控件
- visual c#被Invoke调用的函数内部发生异常时,IDE不能定位到被Invoke函数内部的代码行
- 刚刚学了C#,正巧碰到弹道设计需要大作业,用到了chart控件,发上来纪念一下。
- c#中,确保数据接收完整的 串口处理程序
- C# BackgroundWorker对象 异步执行,避免界面卡死
- C# yield return 用法与解析
- C#打开文件对话框
- C# yield
- CLR via C#深解笔记五 - 事件