C#中值传递问题
来源:互联网 发布:化妆软件哪个好 编辑:程序博客网 时间:2024/05/29 20:03
1.传递后创建新区域,对原值不做任何改变
using System;namespace CsTest{ class Program { public void Swap(int num1,int num2) { int temp; temp = num1; num1 =num2; num2 = temp; } static void Main(string[] args) { int num1; int num2; Program Swap= new Program(); Console.WriteLine("输入NUM1:"); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("输入NUM2:"); num2 = Convert.ToInt32(Console.ReadLine()); Swap.Swap(num1,num2); Console.WriteLine("NUM1="+num1+"\nNUM2="+num2); } }}
2.使用ref,不开辟新内存,改变原值
using System;namespace CsTest{ class Program { public void Swap(ref int num1,ref int num2) { int temp; temp = num1; num1 =num2; num2 = temp; } static void Main(string[] args) { int num1; int num2; Program Swap= new Program(); Console.WriteLine("输入NUM1:"); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("输入NUM2:"); num2 = Convert.ToInt32(Console.ReadLine()); Swap.Swap(ref num1,ref num2); Console.WriteLine("NUM1="+num1+"\nNUM2="+num2); } }}
3.输出参数(可返回多个参数值)
using System;namespace CsTest{ class Program { public void getValue(out int num1,out int num2) { Console.WriteLine("输入NUM1:"); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("输入NUM2:"); num2 = Convert.ToInt32(Console.ReadLine()); } static void Main(string[] args) { int num1; int num2; Program Swap= new Program(); Swap.getValue(out num1,out num2); Console.WriteLine("NUM1="+num1+"\nNUM2="+num2); } }}
阅读全文
0 0
- C#中值传递问题
- Java 中值传递问题
- java中值传递问题
- C#中值传递和引用传递
- Java中值传递和引用传递问题
- C#中值与引用的传递
- javascript中值传递,地址传递,引用传递的问题。
- C#中值传递与引用传递的区别
- C#中值传递与引用传递的区别 .
- C#中值传递与引用传递的区别
- 关于python中值传递和引用传递的问题
- Java中值传递和引用传递的问题
- javascript中值传递,地址传递,引用传递的问题(使用js创建list对象时会用到)
- Java中值传递
- Java中值传递探讨
- java中值传递解析
- 黑马程序员训练营--浅谈c#中值传递和引用传递
- Java中值传递 引用传递
- android开发板
- azure最佳实践系列1-自我修复的设计
- ant---
- Keepalived + nginx实现高可用负载均衡
- 二叉树--求二叉树中两个节点的最近公共祖先
- C#中值传递问题
- Minigame Learning for Day 7
- 线段树两题 hdu1166 hdu1754
- OC与JS交互---JavaScriptCore
- JAVA微信公众号开发之自动回复消息与图片
- Zookeeper来由
- 【HDU3853】LOOPS
- Python数据分析学习笔记(二)
- Event Loop