c#中的Out, params,ref 细说并沉淀
1. Out,params,ref之前先记录平时用的最多的按值传递参数的情况,当然默认情况下参数传入函数的默认行为也是按值传递的。
1: //默认情况下参数会按照值传递
2: static int add(int x,int y) {
3: int ans = x + y;
4: x = 1000; y = 2000;
5: return ans;
6: }
1: static void Main(string[] args) {
2: Console.WriteLine("默认情况下按值传递");
3: int x = 3, y = 8;
4: Console.WriteLine("调用前:x:{0},y:{1}",x,y);
5: Console.WriteLine("调用后的结果:{0}",add(x,y));
6: Console.WriteLine("调用后:x:{0},y:{1}",x,y);
7: Console.ReadLine();
8: }
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
输出的结果跟我们预期的一样:

2.Out修饰符
1: static void add(int x,int y,out int ans) {
2: ans = x + y;
3: }
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
Main里面可以这么写:
1: int ans;
2: add(20, 20, out ans);
3: Console.WriteLine("20+20={0}", ans);
4: Console.ReadLine();
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
输出的结果当然是:20+20=40啦,这个在原来如果没有用out的话那会出现“使用了未赋值的局部变量”。
那即使我们给ans赋值如:int ans=10;
它在调用add方法之后也不会改变,因为调用的方法根本不知道里面发生了什么。
这样就变成了20+20=10了,显然是错的。
当然Out的最大的亮点,就是在一个函数调用之后可以输出多个参数值。
将上面的方法改为:
1: static void setParams(out int x,out string y,out bool ans) {
2: x = 2;
3: y = "YeanJay";
4: ans = true;
5: }
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
我们在Main里面可以这么写:
1: int x=3;
2: string y = "ken";
3: bool ans = false;
4: setParams(out x,out y,out ans);
5: Console.WriteLine("x:{0},y:{1},ans:{2",x,y,ans);
结果:

显然在调用setParams之后相应的变量已经改变了。
这里对Out要注意的一点,在方法体内部必须对加有out 的参数赋值,不然会出现编译错误:必须对输出类型的参数赋值。
3.ref修饰符
最重要的一点,也是跟Out不同的一点,就是使用ref修饰符的时候需要事先对需要ref的参数赋值,不然就会出现对没有赋值的参数进行
操作的编译错误,而out是可以在事先不对变量进行赋值。
看方法:
1: static void SwapStrings(ref string str1,ref string str2) {
2: string temp;
3: temp = str1;
4: str1 = str2;
5: str2 = temp;
6: }
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
方法很简单,就是交换两个字符串。
看Main里面吧,可以这么写:
1: string str1 = "Yean";
2: string str2 = "Jay";
3: Console.WriteLine("调用前:str1:{0},str2:{1}",str1,str2);
4: SwapStrings(ref str1, ref str2);
5: Console.WriteLine("调用后:str1:{0},str2:{1}",str1,str2);
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
结果:

4.params修饰符
说白了就是一个数据作为参数。
直接看方法:
1: static double CalculateAverage(params double[] values) {
2: Console.WriteLine("字符串的长度:{0}",values.Length);
3: double sum = 0;
4: if (values.Length == 0) {
5: return sum;
6: }
7: else {
8: foreach (var value in values) {
9: sum += value
10: }
11: return (sum / values.Length);
12: }
13: }
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
一个求平均值的方法,也很简单。
然后看下Main中的写法:
1: double[] data = { 2.2,2.5,1.1,5.3};
2: double average = CalculateAverage(data);
3: Console.WriteLine("最后的平均值:{0}",average);
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
结果就不写了。
这里需要注意的:C#中只支持一个params参数,而且必须是放在参数列表的最后一个参数,为了避免歧义。
c#中的Out, params,ref 细说并沉淀的更多相关文章
- c#中可变参数params关键字学习
引用 https://www.cnblogs.com/maowp/p/8134342.html 基础知识 1.概念 params 是C#开发语言中关键字, params主要的用处是在给函数传参数的时候 ...
- c#中可变参数(params关键字的使用)
一.params 是C#开发语言中关键字, params主要的用处是在给函数传参数的时候用,就是当函数的参数不固定的时候. 在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中 ...
- (Gorails视频)使用推广链接(params[:ref]),增加注册用户!
用一个链接进行用户的注册推广: 我的git: https://github.com/chentianwei411/embeddable_comments 用途:比如推广,拉朋友注册,给推广码,用这 ...
- C#的参数修饰符out,params,ref
using System; namespace ParamsProgram { class TestParams { public static void Main(string[] args)//s ...
- c#中关键词out和ref的区别
c#中关键词out和ref用来表明以传引用的方式传递参数. 区别如下: 如果方法的参数用out标记,表示方法被调用前不需初始化参数,方法内不能读取此参数的值,在方法返回前必须向此参数写入值: 如果方法 ...
- c#.net中参数修饰符ref,out ,params解析
params ============================================================================================= ...
- C#中的out参数/ref参数/params可变参数
out参数: out关键字 通过引用来传递参数,在定义方法和调用方法的时候都必须使用out关键字 简单来讲out可以用来返回多个参数类型. static void Main(string[] args ...
- C#中的out、ref、params详解
out参数: 如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组.但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,我们可以考虑使用out参数.out参数就侧重于 ...
- C#中三个关键字params,Ref,out
关于这三个关键字之前可以研究一下原本的一些操作 using System; using System.Collections.Generic; using System.Text; namespace ...
随机推荐
- Vue使用总结
好久没更新博客,确实是自己已经懒癌晚期,最近毕业刚工作3个月,公司开发一直在用Vue,自己个人也比较喜欢这个框架,今天就对自己学习到和用到的知识点作一些总结,希望能帮到大家. Vue 知道Vue也一定 ...
- webpack2使用ch10-处理图片(png jpg svg 等) 限制图片 压缩图片
1 目录展示 安装依赖 "file-loader": "^0.11.1", "image-webpack-loader": "^3 ...
- Java面向对象 包
Java面向对象 包 知识概要: (1)包的概念 (2)包的命名 (3)编译执行 (4)导入 (5)包的访问权限 包: 包(package)用于将完成不同功能 ...
- Spring事务管理配置示例
(一).Spring事务特性 1.事务隔离级别 隔离级别是指若干个并发的事务之间的隔离程度. ISOLATION_DEFAULT:默认值,使用数据库的默认隔离级别,就是ISOLATION_READ_C ...
- C# 多线程、异步线程、线程池相关知识
/* 线程池ThreadPool类会在需要时增减池中线程的线程数,直到最大的线程数.池中的最大线程数是可配置的. 在双核CPU中,默认设置为1023个工作线程和1000个I/O线程.也可以指定在创建线 ...
- JavaScript面向对象基础与this指向问题
前 言 我们的程序语言经历了从"面向机器".到"面向过程".再到"面向对象"的一个过程.而JavaScript是一 ...
- win10 uwp 读取保存WriteableBitmap 、BitmapImage
我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap.关于 BitmapImage 和 WriteableBitmap 区别,我就不在这里说.主要说的 ...
- Spring装配Bean之Java代码装配bean
尽管通过组件扫描和自动装配实现Spring的自动化配置很方便也推荐,但是有时候自动配置的方式实现不了,就需要明确显示的配置Spring.比如说,想要将第三方库中的组件装配到自己的应用中,这样的情况下, ...
- VS2008中C#开发webservice简单实例
1.创建工程 文件-> 新建->网站 如下图. 工程建好后,会自动添加如下代码: using System; using System.Linq; using System.Web; us ...
- Milking Time
Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...