当前位置:网站首页>C# 详解out输出参数
C# 详解out输出参数
2022-07-17 03:25:00 【Curz酥】
out参数和ref参数类似,都是让形参成为实参的别名,具体的这里就不再详细描述,可以参考以下博客:C# 图解ref关键字按引用传递参数。下面上简单的代码来看下out参数的作用:
使用out前:
class Program
{
static void Method(int v)
{
v = 20; //对v赋值无法使得a1变成20
}
static void Main()
{
int a1 = 10;
Method(a1);
Console.Write(a1); //输出10
}
}使用out后:
class Program
{
static void Method(out int v) //添加out的地方
{
v = 20; //对输出参数v赋值,这里就可以改变a1的值了
}
static void Main()
{
int a1 = 10; //声明out后,实参的初始值10不会传入方法,所以这里的赋值没有必要
Method(out a1); //添加out的地方
Console.Write(a1); //输出20
}
}注意:由于实参的初始值不会传入方法,那么如果方法中有任何执行路径视图在方法给输出参数赋值之前读取它的值,编译器就会报错:
class Program
{
static void Method(out int v)
{
int temp = v; //报错!因为在方法内输出变量v并未被赋值,所以无法读取输出变量并赋值给temp
}
static void Main()
{
int a1 = 10; //声明out后,实参的初始值10并不会传入方法内
Method(out a1);
Console.Write(a1);
}
}总结:ref和out有一个不同点,即ref要求在传递之前初始化变量,而out不用,这意味着声明out修饰符后,必须要在方法内用代码对out修饰的形参进行赋值。
当然out参数还有一个用途,即通过使用out参数,可以让方法返回多个值:
class Program
{
static void Method(out int v, out string s, out double d) //注意out的使用
{
v = 1;
s = "hello";
d = 3.1415926;
}
static void Main()
{
int a1;
string s1;
double d1;
Method(out a1, out s1, out d1); //注意out的使用
Console.WriteLine(a1);
Console.WriteLine(s1);
Console.WriteLine(d1);
}
}输出结果:
1
hello
3.1415926
边栏推荐
- Digital twinning - Chapter 2, digital twinning Technology
- Asp. Using grpc in NETCORE
- micro、M3O微服务系列(三)
- 论文精读系列文章
- 51 single chip microcomputer - double byte multiplied by double byte
- Laravel's file upload
- 机器学习10:集成学习
- EAS(能量感知调度)绿色节能调度器
- Chapter 2 performance platform godeye source code analysis - data module
- Wechat online education video on demand learning applet graduation project (4) opening report
猜你喜欢

巧用企业网盘收取报告或总结

PAC十年:见证HPC从CPU时代走向XPU纪元

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(3)后台功能

sql界面切换不能获取焦点

Unity - how to modify a package or localize it

小程序毕设作品之微信在线教育视频点播学习小程序毕业设计(1)开发概要

Matlab drawing activation function sigmoid, relu

Wechat online education video on demand learning applet graduation project (4) opening report

A Tutorial on Learned Multi-dimensional Indexes

【黄啊码】MySQL入门—5、数据库小技巧:单个列group by就会,多个列呢?
随机推荐
Introduction to PLC OPC information model (DI, PLCopen nodesets)
[Paper Abstract] screenshots of methods for recording abstracts of interest and papers in special fields.
Use of anti shake debounce and throttling throttle
Mathematical modeling learning (67): detailed introduction to xgboost classification model case tutorial
hello world驱动
Chapter 3 performance platform godeye source code analysis - memory module
Unity Shader - “快速“ 次散射 (Fast SSS : Fast Subsurface Scattering)
Accumulation of natural language processing knowledge points
Redis data migration: Method 1 RDB
Laravel's file upload
Leetcode 931: minimum sum of descent path
Awesome. It turns vscode into idea. It's a little wow
string扩展方法使用
C'est génial de jouer vscode comme un effet idea.
Program analysis and Optimization - 11 multi branch analysis
Micro, m3o micro service series (III)
A Tutorial on Learned Multi-dimensional Indexes
机器学习11:代价敏感学习
Klakndi synchronization screen is simple to use
机器学习10:集成学习