当前位置:网站首页>C # explain out output parameters in detail
C # explain out output parameters in detail
2022-07-19 04:13:00 【Curz crisp】
out Parameters and ref The parameters are similar to , All are Let the formal parameter become the alias of the argument , I won't describe it in detail here , You can refer to the following blog :C# The illustration ref Keyword passes parameters by reference . Let's take a look at the simple code out Function of parameters :
Use out front :
class Program
{
static void Method(int v)
{
v = 20; // Yes v Assignment cannot make a1 become 20
}
static void Main()
{
int a1 = 10;
Method(a1);
Console.Write(a1); // Output 10
}
}Use out after :
class Program
{
static void Method(out int v) // add to out The place of
{
v = 20; // For output parameters v assignment , Here you can change a1 The value of the
}
static void Main()
{
int a1 = 10; // Statement out after , The initial value of the argument 10 No method will be passed , So the assignment here is unnecessary
Method(out a1); // add to out The place of
Console.Write(a1); // Output 20
}
}Be careful : Because the initial value of the argument will not be passed into the method , Then if there is any execution path in the method, the view reads its value before the method assigns a value to the output parameter , The compiler will report an error :
class Program
{
static void Method(out int v)
{
int temp = v; // Report errors ! Because output variables in methods v Not assigned , Therefore, the output variable cannot be read and assigned to temp
}
static void Main()
{
int a1 = 10; // Statement out after , The initial value of the argument 10 It will not be passed into the method
Method(out a1);
Console.Write(a1);
}
}summary :ref and out There is a difference , namely ref Require variables to be initialized before passing , and out no need , It means to declare out After the embellishment , You have to code in the method out Modify the formal parameters to assign values .
Of course out Parameters have another purpose , By using out Parameters , You can make the method Return multiple values :
class Program
{
static void Method(out int v, out string s, out double d) // Be careful out Use
{
v = 1;
s = "hello";
d = 3.1415926;
}
static void Main()
{
int a1;
string s1;
double d1;
Method(out a1, out s1, out d1); // Be careful out Use
Console.WriteLine(a1);
Console.WriteLine(s1);
Console.WriteLine(d1);
}
}Output results :
1
hello
3.1415926
边栏推荐
- [database] must know at the end of the term ----- Chapter 2 relational data model
- 7.16 simulation summary
- Modify jar package content
- Unity - 如何修改一个 Package 或是如何将 Package Local化
- 小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书
- 牛客2021训练联盟热身训练赛Interstellar Love(并查集)
- Workload-Aware Performance Tuning for Autonomous DBMSs
- 英特尔+联想共同推出开源云解决方案
- How to filter viruses / spam more effectively!
- High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
猜你喜欢

小程序畢設作品之微信在線教育視頻點播學習小程序畢業設計(3)後臺功能

Workload-Aware Performance Tuning for Autonomous DBMSs

Matlab drawing activation function sigmoid, relu

小程序毕设作品之微信电子书阅读小程序毕业设计(8)毕业设计论文模板

Unity Shader - “快速“ 次散射 (Fast SSS : Fast Subsurface Scattering)

A Tutorial on Learned Multi-dimensional Indexes

Chapter 4 performance platform godeye source code analysis - monitoring module

学术分享 | 基于OpenVINO的多染色病理图像信息评估系统设计与开发

【超能云终端创领先机】本地计算云端管理,英特尔助力教育数字化

Skillfully use enterprise network disk to collect reports or summaries
随机推荐
Asp. Using grpc in NETCORE
小程序毕设作品之微信电子书阅读小程序毕业设计(8)毕业设计论文模板
机器学习09:无监督学习
小程序毕设作品之微信电子书阅读小程序毕业设计(3)后台功能
In tech 2022 | Intel technology product innovation quick view
【数据库】期末必知必会-----第九章 数据库设计
Chapter 5 performance platform godeye source code analysis - third party module
Realize the dual opening of wechat on the computer (log in to two wechat)
Typescript数组/对象/字符串/函数参数的解构使用
图形验证码验证
To build agile teams, these methods are indispensable
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
结构体通过成员变量获取主结构体地址(struct)
Codeforces Round #807 (Div. 2) A~D
Which computer painting software works well: try artweaver plus, which is comparable to Sai painting software | download the latest version of artweaver
High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
ASP.NET1==visual studio创建asp.net demo
小程序毕设作品之微信电子书阅读小程序毕业设计(5)任务书
【RuoYi-Vue-Plus】学习笔记 30 - Redisson(六) 有界阻塞队列 Bounded Blocking Queue(Redisson 源码 + Lua 脚本)
无心剑汉英双语诗005.《抒怀》