c# ref与out用法
2024-09-15 07:21:35
class Program
{
static void Main(string[] args)
{
//普通 :
int k1 = ;
int result1 = Test1(k1, );
Console.WriteLine("/*普通:*/");
Console.WriteLine(result1);
Console.WriteLine(k1); //ref :
//用法: 参数要用 ref 一起传递,并且是可以赋值的变量,ref 变量要先赋值
int k2 = ;
int result2 = Test2(ref k2, );
Console.WriteLine("/*ref:*/");
Console.WriteLine(result2);
Console.WriteLine(k2); //out :
//用法: 参数要用 out 一起传递,并且是可以赋值的变量,out 变量不用先赋值(只定义即可)
int k3;
Console.WriteLine("/*out:*/");
int result3 = Test3(out k3, );
Console.WriteLine(result3);
Console.WriteLine(k3);
} /// <summary>
/// 普通
/// </summary>
public static int Test1(int k, int p)
{
k++;
return k + p;
} /// <summary>
/// ref
/// </summary>
public static int Test2(ref int k, int p)
{
k++;
return k + p;
} /// <summary>
/// out
/// </summary>
public static int Test3(out int k, int p)
{
//out 未赋值的变量不能使用,所以在方法内部赋值。
k = + ;
return k + p;
}
}
运行结果:
最新文章
- 兄弟连javascript学习笔记
- 使用 Laravel 前的准备工作
- 在Python中调用glutInit遇到OpenGL.error.NullFunctionError的解决方法
- 整理 iOS 9 适配中出现的坑(图文)(转)
- MVP -----个人理解与示例(android例子 实现)
- BZOJ4060 : [Cerc2012]Word equations
- JavaScript要点(十七) Math 对象
- 5 个在 Linux 中管理文件类型和系统时间的有用命令
- PL/SQL:使用pragma restrict_references限制包权限
- Two ways to create file using &#39;doc&#39;.
- FeatureClass Copy
- 淘淘商城_day11_课堂笔记
- UE4利用Save Game创建全局变量
- flask下载excel
- win10安装配置nodejs
- 【转】C++标准转换运算符const_cast
- Python Matplot中文显示完美解决方案
- Windows操作系统下的MySQL主从复制及读写分离
- 基于Sql Server 2008的分布式数据库的实践
- spark2.1.1创建Pipeline