关键字:this、ref、out
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Class1
{
private string name;
public Class1(string name)
{
// 使用this关键字表明使用当前对象的属性(或方法),后面的name是形参name
this.name = name;
}
public void Eat()
{
Console.WriteLine("名字:" + this.name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 使用类
Class1 myname = new Class1("namejr");
myname.Eat();
}
}
}
使用ref关键字进行引用传参
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int age = ;
// 关键字ref按引用传参
Growth(ref age);
Console.WriteLine(age);
}
// 该static是int类型的++,相当于值类型
static void Growth(ref int age)
{
age++;
Console.WriteLine("int又长大一岁!!!");
}
}
}
使用out关键字做引用传递
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int age = ;
// 关键字out按引用传参
// out可以返回多个属性值,而return只能返回一个
int prev_a, next_a; // 用来接收上一年和下一年的年龄
Growth(age, out prev_a, out next_a);
Console.WriteLine("上一年的年龄:" + prev_a);
Console.WriteLine("明年的年龄:" + next_a);
}
// 使用out关键字,也用来做引用传递
static void Growth(int age, out int prevage, out int nextage)
{
prevage = age - ;
nextage = age + ;
}
}
}
值得注意的是:ref、out都是引用传递,使用的方法在值传递的前面加上ref/out即可;在使用ref前必须对ref所引用的变量进行赋值,否则会报错,out可以不在引用前赋值,但必须在离开方法前赋值。

修改:

关于值传递和引用传递:https://www.cnblogs.com/namejr/p/10261486.html
关键字:this、ref、out的更多相关文章
- C#中三个关键字params,Ref,out
关于这三个关键字之前可以研究一下原本的一些操作 using System; using System.Collections.Generic; using System.Text; namespace ...
- c#关键字及ref和out
最近在写程序时遇到ref,out 参数问题.回头有自习看了看MSDN,才有巩固了基础.我把我的测试程序贴出来,大家分享一下. ref 关键字使参数按引用传递.其效果是,当控制权传递回调用方法时, ...
- 5.C#知识点:ref和Out关键字浅谈
首先我们要知道ref和out在C#里面是什么? 答:它们俩是C#里面的关键字. 他们俩是干啥的呢? 答:他们俩是方法参数的修饰符号,一但使用,方法定义和方法都用都要使用这个关键字,这一点是死规定. 好 ...
- Out 与 Ref 关键字的区别
相同点:既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键字都能够提供相似的功效, ...
- 关于c#中”ref”和”out”关键字的一些理解
一. 综述(本文内容大部分来自网络,经本人整理而成,仅供学习参考,不免理解错误,欢迎批评指正) 在c#中,方法的参数传递有四种类型: (1) 传值参数(by value) 传值参数无需额外的修饰符.传 ...
- 《ASP.NET 1200例》ref关键字与out关键字
REF关键字 ref 关键字会导致通过引用传递的参数,而不是值. 通过引用传递的效果是在方法中对参数的任何改变都会反映在调用方的基础参数中. 引用参数的值与基础参数变量的值始终是一样的. 不要将“通过 ...
- C# 中三个关键字params,Ref,out
一. using System; using System.Collection.Generic; using System.Text; namespace ParamsRefOut { class ...
- C#知识点:ref和Out关键字浅谈
首先我们要知道ref和out在C#里面是什么? 答:它们俩是C#里面的关键字. 他们俩是干啥的呢? 答:他们俩是方法参数的修饰符号,一但使用,方法定义和方法都用都要使用这个关键字,这一点是死规定. 好 ...
- 索引器和ref、out关键字
这节讲三个小知识:索引器.ref.out. 索引器: 在一个类中,我们可以定义一个索引器,它可以让我们在外部像访问数组元素一样访问类的属性成员. 索引器的定义就像定义属性一样,只不过名称为this,后 ...
- C#中ref关键字的用法总结
ref表示引用的意思,C#中它有多种用法,这里简单总结一下: 1.按引用传递参数 具体可见:C#中的值传递与引用传递(in.out.ref) 2.引用局部变量 引用局部变量指的是在变量声明时使用ref ...
随机推荐
- linux的python版本升级
可利用Linux自带下载工具wget下载,如下所示: # wget http://www.python.org/ftp/python/2.7.3/Python-2.7.13.tgz 下载完成后 ...
- 团队作业8——敏捷冲刺博客合集(Beta阶段)
第一篇(冲刺前安排):https://www.cnblogs.com/Aragaki-Yui/p/9057951.html 第二篇(冲刺第一天):https://www.cnblogs.com/Ara ...
- redis 基础应用
redis 安装 -redis安装 -window的安装 -redis支持5大数据类型 -字符 Memcached 只支持字符串类型 -列表 -字典 -集合 -有序集合 面试题:游戏 ...
- python如何进行内存管理的
python引用了一个内存池(memory pool)机制,即pymalloc机制(malloc:n,分配内存),用于管理对小块的申请和释放.
- Python基础5--字符串
1 find().rfind().index().rindex().count() s = "this apple is red apple" s.find("apple ...
- HDU 1004 Let the Balloon Rise(map应用)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- Oracle数据库select语句
select * from EMp--all data in EMP table select * from EMP where ename in('SMITH')--the data where e ...
- mac os x lipo 工具
lipo是管理Fat文件的工具,可以查看平台列表,提取.重新打包 dreamdeMac-mini:test dream$ lipo -info libtest001.a Architectures i ...
- Android SO UPX壳问题小记
网上有篇 Android SO(动态链接库)UPX加固指南,详细介绍了如何使用UPX给Android SO加壳,尝试做了一下结果ok,这里只记录遇到的几个小问题. 1.40k以下so不能加壳 kiii ...
- 九度OJ1108-堆栈的使用
题目1108:堆栈的使用 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11620 解决:3372 题目描述: 堆栈是一种基本的数据结构.堆栈具有两种基本操作方式,push 和 pop.P ...