当前位置:网站首页>Method parameter transfer mechanism of C #
Method parameter transfer mechanism of C #
2022-07-28 09:41:00 【InfoQ】
One 、 Value parameter :(
Value type formal parameter
)
class Program
{
static void Main(string[] args)
{
int a = 3, b = 5;
Console.WriteLine("Main.. a:" + a + "...b:" + b);
Exchange(a, b);
Console.WriteLine("Main.. a:" + a + "...b:" + b);
}
static void Exchange(int a, int b)
{
Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
int temp = a;
a = b;
b = temp;
Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
}
}

Two 、 Reference parameter (
ref keyword
)
class Program
{
static void Main(string[] args)
{
int a = 3, b = 5;
Console.WriteLine("Main.. a:" + a + "...b:" + b);
Exchange(ref a,ref b);
Console.WriteLine("Main.. a:" + a + "...b:" + b);
Console.ReadKey();
}
static void Exchange(ref int a,ref int b)
{
Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
int temp = a;
a = b;
b = temp;
Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
}
}

3、 ... and 、 Output parameter (
out keyword
)
class Program
{
static void Main(string[] args)
{
int[] arr = new int[5] { 1, 2, 3, 4, 5 };
float average;
int sum = Calculation(arr, out average);
Console.WriteLine(" The sum of these numbers is :" + sum + ".. The average is :" + average);
Console.ReadKey();
}
static int Calculation(int[] arr,out float average)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
average = sum / arr.Length;
return sum;
}
}

class Program
{
static void Main(string[] args)
{
int[] arr = new int[5] { 1, 2, 3, 4, 5 };
Console.WriteLine("Main Before calling the method :");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
Reference(arr);
Console.WriteLine();
Console.WriteLine("Main After calling method :");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.ReadKey();
}
static void Reference(int[] arr)
{
Console.WriteLine("Reference Come in :");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
for (int i = 0; i < arr.Length; i++)
{
arr[i] = 0;
}
Console.WriteLine();
Console.WriteLine("Reference After modification :");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
}

C# Positional and named parameters in
class Program
{
static void Main(string[] args)
{
int a = 3, b = 5;
Console.WriteLine("Main.. a:" + a + "...b:" + b);
//Exchange(a, b);
Exchange(b: b, a: a);
Console.WriteLine("Main.. a:" + a + "...b:" + b);
Console.ReadKey();
}
static void Exchange(int a,int b)
{
Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
int temp = a;
a = b;
b = temp;
Console.WriteLine("Exchange.. a:" + a + "...b:" + b);
}
}

边栏推荐
- 《我的Vivado实战—单周期CPU指令分析》
- Regular expressions for positive and negative values
- 【解决】ERROR in [eslint] ESLint is not a constructor
- [Guangxi University] information sharing of postgraduate entrance examination and re examination
- Analysis of HashSet internal principle
- ArrayList内部原理解析
- LeetCode_ 406_ Rebuild the queue based on height
- MySQL中各类型文件详解
- What is cross domain? How to solve the cross domain problem?
- MATLAB的实时编辑器
猜你喜欢

Go language slice vs array panic runtime error index out of range problem solving

JDBC connection database

Introduction to shardingsphere (I)

opencv安装配置测试

21天学习挑战赛-《Autosar从入门到精通-实战篇》

IJCAI 2022 | the latest overview of graph structure learning: research progress and future prospects

译文推荐 | 调试 BookKeeper 协议 - 无界 Ledger

Promise实例如何解决地狱回调

Introduction to shardingsphere's concept of sub database and sub table (2)

【广西大学】考研初试复试资料分享
随机推荐
Regular expressions are hexadecimal digits?
478-82(56、128、718、129)
数据库那么多概念性的东西怎么学?求方法
2.9.5 ext JS object type processing and convenient methods
2022 safety officer-b certificate examination simulated 100 questions and answers
Basic operation of MATLAB
[summary of leetcode weekly competition] the 83rd fortnight competition of leetcode (7.23)
ShardingSphere之分库分表概念介绍(二)
ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
mq的学习
Hexadecimal representation of negative numbers
就这么一个简单的校验,80%的程序员却做不到,更不理解!
Source code analysis of view event distribution mechanism
QT基础练手小程序-简单计算器设计(附带源码,解析)
express搭建一个简易的本地后台(一)
Dn-detr paper accuracy, and analyze its model structure & 2022 CVPR paper
【日志】日志干什么的?日志工厂是什么?log4j 的配置和使用? log4j.properties 文件配置、log4j jar包坐标
MySQL中各类型文件详解
MATLAB启动慢解决措施
Informatics Olympiad all in one 1617: circle game | 1875: [13noip improvement group] circle game | Luogu p1965 [noip2013 improvement group] circle game