当前位置:网站首页>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);
}
}

边栏推荐
- C# 窗体应用使用对象绑定 DataGridView 数据绑定
- 股指期货开户的条件和流程
- ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
- 2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
- Conditions and procedures of stock index futures account opening
- Hexadecimal representation of negative numbers
- Talk to the father of MySQL: code completion at one time is a good programmer
- Technology sharing | quick intercom integrated dispatching system
- 5 operators, expressions, and statements
- ECCV 2022 | can be promoted without fine adjustment! Registration based anomaly detection framework for small samples
猜你喜欢

IntelliJ idea associated database

Express builds a simple local background (1)

Oracle-11gr2 default system job

2022 high voltage electrician examination simulated 100 questions and simulated examination

Technology sharing | quick intercom integrated dispatching system

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

What is cross domain? How to solve the cross domain problem?

业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)

2022 safety officer-b certificate examination simulated 100 questions and answers

Personal blog applet
随机推荐
Technology sharing | quick intercom integrated dispatching system
QT basic hand training applet - simple calculator design (with source code, analysis)
什么是跨域?如何解决请跨域问题?
C# 之 方法参数传递机制
2022 high voltage electrician examination simulated 100 questions and simulated examination
ECCV 2022 | 无需微调即可推广!基于配准的少样本异常检测框架
Promise实例如何解决地狱回调
[JVM] JVM refers to floating point number
ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
Express builds a simple local background (1)
How view works
[autosar-rte] - 3-runnable and its task mapping mapping
2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
Network engineering -- ranking of Chinese universities in Soft Science
matlab基本操作
MATLAB的数列与极限运算
oracle 创建用户且只有查询权限
Analysis of HashSet internal principle
DN-DETR 论文精度,并解析其模型结构 & 2022年CVPR论文
Regular expressions are hexadecimal digits?