C# 中三个关键字params,Ref,out
一、
using System;
using System.Collection.Generic;
using System.Text;
namespace ParamsRefOut
{
class Program
{
static void ChangeValue(int i)
{
i=5;
Console.WriteLine("The ChangeValue method changed the value "+i.ToString());
}
static void Main(string[] args)
{
int i=10;
Console.WriteLine("The value of I is"+i.ToString());
ChangeValue(i);
Console.WriteLine("The value of I is"+i.ToString());
Console.ReadLine();
}
}
}
本文主要讨论params关键字,ref关键字,out关键字
1)params关键字,官方给出的解释为用于方法参数长度不定的情况。有时候不能确定一个方法的方法参数到底有多少个,可以使用params关键字来解决问题
using System;
using System.Collections.Generic;
using System.Text;
namespace ParamsRefOut
{
class number
{
public static void UseParams(params int[] list)
{
for(int i=0;i<list.Length;i++)
{
Console.WriteLine(list[i]);
}
}
static void Main(string[] args)
{
UseParams(1,2,3);
int[] myArray=new int[3]{10,11,12};
UseParams(myArray);
Console.ReadLine();
}
}
}
2)ref关键字:使用引用类型参数,在方法中对参数所做的任何更改都将反应在该变量中
using System;
using System.Collections.Generic;
using System.Text;
namespace ParamsRefOut
{
class number
{
static void Main()
{
int val=0;
Method(ref val);
Console.WriteLine(val.ToString());
}
static void Method(ref int i)
{
i=44;
}
}
}
(3)out关键字:out与ref相似但是out无需进行初始化
C# 中三个关键字params,Ref,out的更多相关文章
- C#中三个关键字params,Ref,out
关于这三个关键字之前可以研究一下原本的一些操作 using System; using System.Collections.Generic; using System.Text; namespace ...
- C#中的参数关键字params
class 参数 { public void doSome(string str,params int[] values){ ) { ; i < values.Length; i++) { Co ...
- (Gorails视频)使用推广链接(params[:ref]),增加注册用户!
用一个链接进行用户的注册推广: 我的git: https://github.com/chentianwei411/embeddable_comments 用途:比如推广,拉朋友注册,给推广码,用这 ...
- 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)
浅谈c#的三个高级参数ref out 和Params c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和 ...
- 浅谈c#的三个高级参数ref out 和Params
c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和Params,在这里我们来分别的讲解一下,在这里的我们先不 ...
- PHP中this,self,parent三个关键字
this,self,parent三个关键字从字面上比较好理解,分别是指这.自己.父亲. this是指向当前对象的指针(姑且用C里面的指针来看吧)self是指向当前类的指针parent是指向父类的指针( ...
- c#中的Out, params,ref 细说并沉淀
1. Out,params,ref之前先记录平时用的最多的按值传递参数的情况,当然默认情况下参数传入函数的默认行为也是按值传递的. 1: //默认情况下参数会按照值传递 2: static int a ...
- C/C++中static,const,inline三种关键字详细总结
一.关于staticstatic 是C++中很常用的修饰符,它被用来控制变量的存储方式和可见性,下面我将从 static 修饰符的产生原因.作用谈起,全面分析static 修饰符的实质. static ...
- php中this,self,parent三个关键字的区别辨析
php中this,self,parent三个关键字的区别辨析 一.总结 一句话总结:this是指向对象实例的一个指针,self是对类本身的一个引用,parent是对父类的引用. 1.self关键字使用 ...
随机推荐
- Python之数据结构:元组
一.元组 tupleA=(1,2,3,4,5,6) print tupleA 1.元组支持的运算符 tup1+tup2 tup1*2 3 in tup2 for i in tup2: print i ...
- ClassLoader 提供了两个方法用于从装载的类路径中取得资源:
转:http://cheneyph.iteye.com/blog/831721 ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL getResource ( ...
- 当文字过长时裁剪(显示省略号或只裁剪 用CSS方法,不用程序)
原文发布时间为:2009-09-16 -- 来源于本人的百度文章 [由搬家工具导入] CSS中ellipsis()应用【转】 CSS手册中text-overflow属性的定义: 语法: text- ...
- 《手把手教你学C语言》学习笔记(1)---C语言的特点
学习C语言的原因,主要是需要使用C语言编程,我用故我学,应该是最主要的原因了. C语言的定位:C语言严格意义上只能算是中级语言,是面向过程编程语言的集大成者,虽然这种语言有很多的问题,但总体而言是瑕不 ...
- Manjaro linux软件源设置
1.从官方http://jaist.dl.sourceforge.net/project/manjarotest/16.06-dev/kde/minimal/manjaro-kde-minimal-1 ...
- mac使用matplotlib
mac系统,安装了matplotlib之后, 导入matplotlib.pyplot的时候报错 RuntimeError: Python is not installed as a framework ...
- Java IO 学习(零)概述
参考文献 Doug Lea的 <Scalable IO in Java> Java NIO trick and trap IO - 同步,异步,阻塞,非阻塞 (亡羊补牢篇) 网络编程释疑 ...
- LOJ N!在不同进制的位数
lightoj1045 - Digits of Factorial (N!不同进制的位数) 对于一个B进制的数,只需要对其取以B的对数就可以得到他在B进制情况下的位数(取了对数之后可能为小数,所以还需 ...
- 转:java多线程CountDownLatch及线程池ThreadPoolExecutor/ExecutorService使用示例
java多线程CountDownLatch及线程池ThreadPoolExecutor/ExecutorService使用示例 1.CountDownLatch:一个同步工具类,它允许一个或多个线程一 ...
- Sort Detail Data Block Example - Oracle Forms
Example is given below to sort detail data block data (toggle asc or desc) with push buttons used as ...