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的更多相关文章

  1. C#中三个关键字params,Ref,out

    关于这三个关键字之前可以研究一下原本的一些操作 using System; using System.Collections.Generic; using System.Text; namespace ...

  2. c#关键字及ref和out

    最近在写程序时遇到ref,out 参数问题.回头有自习看了看MSDN,才有巩固了基础.我把我的测试程序贴出来,大家分享一下.    ref 关键字使参数按引用传递.其效果是,当控制权传递回调用方法时, ...

  3. 5.C#知识点:ref和Out关键字浅谈

    首先我们要知道ref和out在C#里面是什么? 答:它们俩是C#里面的关键字. 他们俩是干啥的呢? 答:他们俩是方法参数的修饰符号,一但使用,方法定义和方法都用都要使用这个关键字,这一点是死规定. 好 ...

  4. Out 与 Ref 关键字的区别

    相同点:既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键字都能够提供相似的功效, ...

  5. 关于c#中”ref”和”out”关键字的一些理解

    一. 综述(本文内容大部分来自网络,经本人整理而成,仅供学习参考,不免理解错误,欢迎批评指正) 在c#中,方法的参数传递有四种类型: (1) 传值参数(by value) 传值参数无需额外的修饰符.传 ...

  6. 《ASP.NET 1200例》ref关键字与out关键字

    REF关键字 ref 关键字会导致通过引用传递的参数,而不是值. 通过引用传递的效果是在方法中对参数的任何改变都会反映在调用方的基础参数中. 引用参数的值与基础参数变量的值始终是一样的. 不要将“通过 ...

  7. C# 中三个关键字params,Ref,out

    一. using System; using System.Collection.Generic; using System.Text; namespace ParamsRefOut { class ...

  8. C#知识点:ref和Out关键字浅谈

    首先我们要知道ref和out在C#里面是什么? 答:它们俩是C#里面的关键字. 他们俩是干啥的呢? 答:他们俩是方法参数的修饰符号,一但使用,方法定义和方法都用都要使用这个关键字,这一点是死规定. 好 ...

  9. 索引器和ref、out关键字

    这节讲三个小知识:索引器.ref.out. 索引器: 在一个类中,我们可以定义一个索引器,它可以让我们在外部像访问数组元素一样访问类的属性成员. 索引器的定义就像定义属性一样,只不过名称为this,后 ...

  10. C#中ref关键字的用法总结

    ref表示引用的意思,C#中它有多种用法,这里简单总结一下: 1.按引用传递参数 具体可见:C#中的值传递与引用传递(in.out.ref) 2.引用局部变量 引用局部变量指的是在变量声明时使用ref ...

随机推荐

  1. php 正则概述

    修饰符   s 模式中的点号元字符匹配所有字符 非打印字符 字符  含义  \cx  匹配由x指明的控制字符.例如, \cM 匹配一个 Control-M 或回车符.x 的值必须为 A-Z 或 a-z ...

  2. mail命令入门及进阶

    mail是linux shell中的邮件工具,与crontab配合使用,可以实现定期发送邮件.本文主要介绍mail工具使用方法及注意事项. 1.mail命令一般用法: mail –s "邮件 ...

  3. 12.输入一个成绩计算其A,B,C,D,E等级

    #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int score; s ...

  4. chrome 总崩溃的正确解决方法

    解决办法: 原因就是 C:\Windows\System32\drivers\bd0001.sys 这个文件 可以把这个文件删除,或者重命名,删除或者重命名后一定要重启电脑,再打开Chrome就OK了 ...

  5. 微软Power BI 每月功能更新系列——9月Power BI 新功能学习

    Power BI Desktop 9月新功能摘要 Power BI 9月更新如期而至,这一次Power BI 又推出了新功能——聚合预览,它可在内存中无缝地存储汇总值,大大提高报告的性能.另外本月还包 ...

  6. tomcat自动缓存的几种解决方式

    第一种方法:打开一个项目,这里我打开的Mail项目,然后点击Myeclipse菜单栏中的project-选择clean: 选择要clean的项目,确定即可不用进入tomcat服务器直接清理缓存. 上面 ...

  7. 【转载】 Pytorch中的学习率调整lr_scheduler,ReduceLROnPlateau

    原文地址: https://blog.csdn.net/happyday_d/article/details/85267561 ------------------------------------ ...

  8. C++指针易错点梳理

    1 指针定义 指针是一个变量:指针的值是另一个变量的地址.变量的声明 type *var-name; var-name 是指针变量的名称.星号是用来指定一个变量var-name是指针变量. int * ...

  9. 查询表Or列的注释信息

    需求:开发人员需要DBA支持,查询表的注释说明,用于明确表的用途. 1.测试 session 1 创建测试表SQL> create table a_emp as select * from sc ...

  10. 将数据存入mysql中

    import pymysql import warnings # 忽略警告 warnings.filterwarnings("ignore") # 连接数据库 db = pymys ...