(C#基础) ref 和out练习
对于C#中这两个关键字的用法,常常混淆,有点不清楚,今天又一次看到。遂把它们都记录下来,希望能有所用。这些都是他人写的,我只是搬过来一次,加深印象。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace dazilianxi.wenjian
{
public class MoTes:IEnumerable<SanWei>
{
private readonly List<SanWei> _motes;
public MoTes()
{
_motes = new List<SanWei>();
} public void Add(double xiong,double yao,double tun)
{
_motes.Add(new SanWei(xiong,yao,tun));
}
#region IEnumerable<SanWei> 成员 public IEnumerator<SanWei> GetEnumerator()
{
//throw new NotImplementedException(); return _motes.GetEnumerator();
} #endregion #region IEnumerable 成员 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
// throw new NotImplementedException();
return GetEnumerator();
} #endregion
}
//一定要灵活使用
public struct SanWei
{
public readonly double _Xiong;
public readonly double _Yao;
public readonly double _Tun;
public SanWei(double xiong,double yao,double tun)
{
this._Xiong = xiong;
this._Yao = yao;
this._Tun = tun;
}
} public class Pet
{
public int Age { get; set; }
public static void ChageAge(Pet p)
{
p.Age = ;
} public static void ChangePet(ref Pet p)
{
p = new Pet()
{
Age=
};
}
} public class Player
{
public int Id { get; set; }
public string Name{get;set;}
public string Position { get; set; }
public bool IsbestPlayer { get; set; }
} public class Team
{
public Team()
{
listPlay = new List<Player>();
}
public int Id { get; set; }
public string Name { get; set; }
public int ScoreCount { get; set; }
public List<Player> listPlay { get; set; } /*
out和ref:
● 相同的地方在于:传递的引用
● 不同之处在于:ref在使用之前需要赋上初值,out可以赋初值也可以不赋
*/
//out为了在方法内改变值,然后在外面调用,ref 是作为一个判断条件,在方法里用,也可以发生改变
public static void WhoWinWorldCup(Team a, Team b, out string bestPlayer)
{
if(a.ScoreCount>b.ScoreCount)
{
Console.WriteLine("恭喜{0}对,赢得了这次比赛",a.Name);
}
else
{
Console.WriteLine("恭喜{0}对,赢得了这次比赛",a.Name);
}
bestPlayer=LookForBestPlayer(a,b);
} public static string LookForBestPlayer(Team a ,Team b)
{
string result = string.Empty;
//把Team b的球员合并到Team a中去
a.listPlay.AddRange(b.listPlay);
foreach( var plays in a.listPlay)
{
if(plays.IsbestPlayer==false)
{
continue;
}
else
{
result = plays.Name;
break;
}
}
return result;
}
}
}
main 中
/* var list = new MoTes()
{
{79, 60, 89},
{82, 63, 90}
};
foreach (var item in list)
{
Console.WriteLine("胸围:{0},腰围:{1},臀围:{2}", item._Xiong, item._Yao, item._Tun);
}
*/
/*
//ref 使用前必须赋值,out不需要
//out必须在方法里赋值,外面赋值不起作用,ref 在里面可以发生变化
//相同点都是取变化值灵活调用 Pet p = new Pet() { Age = 5 };
Console.WriteLine("初始年龄是:{0}", p.Age);
Pet.ChageAge(p);
Console.WriteLine("改变pet的属性值后,年龄是:{0}", p.Age);
Pet.ChangePet(ref p);
Console.WriteLine("改变pet引用地址后,年龄是:{0}", p.Age);
Console.ReadKey();*/ Console.WriteLine("央视足球解说员贺炜:欢迎大家来到本届世界杯的决赛现场~~");
Console.WriteLine("央视足球解说员贺炜:决赛的2支队伍是:"); Team brazil = new Team()
{
Id = ,
Name = "巴西队",
listPlay = new List<Player>()
{
new Player(){Id = , Name = "内马尔", Position = "前锋"},
new Player(){Id = , Name = "阿尔维斯", Position = "后卫"}
}
}; Team germany = new Team()
{
Id = ,
Name = "德国队",
listPlay = new List<Player>()
{
new Player(){Id = , Name = "齐勒", Position = "前锋"},
new Player(){Id = , Name = "拉姆", Position = "后卫"}
}
}; Console.WriteLine("来自南美的{0}主场迎战来自欧洲的劲旅{1}", brazil.Name, germany.Name); Console.WriteLine("在比赛的89分钟,德国队前锋齐勒禁区外抽射死角,锁定胜局~~");
germany.listPlay[].IsbestPlayer = true;
germany.ScoreCount = ;
brazil.ScoreCount = ; string best = string.Empty;
Team.WhoWinWorldCup(brazil, germany, out best);
Console.WriteLine("本场比赛的最佳球员是:{0}", best);
Console.WriteLine();
Console.WriteLine("央视足球解说员贺炜:这是牵动人心的90分钟。在这场比赛之后,总有一支球迷热爱的球队要离开,而这场比赛本身,将成为我们记忆中的永恒财富。等我们老去的时候,在壁炉旁抱着自己的孙子,一定会跟他们讲起2014年,讲起今晚的巴德大战。");
Console.ReadKey();
参考:
http://www.cnblogs.com/darrenji/p/3821313.html
http://www.cnblogs.com/darrenji/p/3822000.html
(C#基础) ref 和out练习的更多相关文章
- [C#基础]ref和out的区别
在C#中通过使用方法来获取返回值时,通常只能得到一个返回值.因此,当一个方法需要返回多个值的时候,就需要用到ref和out,那么这两个方法区别在哪儿呢? MSDN: ref 关键字使参数按 ...
- 10. react 基础 ref 的使用 及 React 16 的生命周期函数 及 生命周期函数使用场景
一. ref 的使用 ( 直接获取 DOM 元素 ) 在 input 标签上 可以使用 ref 属性 获取当前DOM节点 eg: import React , { Component, Fragmen ...
- [C#基础]ref和out的使用
在C#中如果需要把值类型转换成引用类型传递其他方法中并使其原来值发生改变,使用 ref 和 out 转换成引用类型传递. 1. ref : 使用ref之前需要定义变量并初始化(必须初始) class ...
- C#基础-ref、out
1.默认情况下,C#假定所有的方法参数传递都是传值的. 如下面的方法: public static void Main(string[] args) { int val = 5; //调用AddVal ...
- C#基础--Ref与Out区别
两者都是按地址传递的,使用后都将改变原来参数的数值. class Program { static void Main(string[] args) { int num = 1; Method(ref ...
- vue基础 ref的作用
1. ref 获取dom元素,除了能获取dom元素也能获取组件dom, 组件通信: 在父组件中直接调用ref定义的组件的数据或者方法 <div id="app&qu ...
- [Laravel] 14 - REST API: Laravel from scratch
前言 一.基础 Ref: Build a REST API with Laravel API resources Goto: [Node.js] 08 - Web Server and REST AP ...
- Python :数据结构
LearnPython :数据结构 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .labe ...
- Java.Annotations
Annotation 0. Annotation Tricks http://developer.android.com/reference/java/lang/annotation/Annotati ...
随机推荐
- DDR2是什么意思
DDR2DDR2(Double Data Rate 2) SDRAM是由JEDEC(电子设备工程联合委员会)进行开发的新生代内存技术标准,它与上一代DDR内存技术标准最大的不同就是,虽然同是采用了在时 ...
- <OFFER03>03_01_DuplicationInArray
#include<cstdio> bool duplicate(int numbers[], int length, int* duplication) { ) return false; ...
- 9.1C#中类的定义
9.1 C#中类的定义 C#使用class关键字来定义类 [默认internal] class MyClass { //Class Members } 在默认情况下,类声明为内部的,即只有当前项目 ...
- 将DevExpress.Utils.ImageCollection变量的image导出
private void tspBtnExportExcel_Click(object sender, EventArgs e) { //暂时用来导出图片 string filePath = Syst ...
- org.apache.axis2.AxisFault: unknown
遇到这个异常懵逼了很长时间才解决,axis2框架个人感觉进行接口相互调用还是比较麻烦的, 调了很长时间,我由a项目调用b项目的接口时,一直报这个错,在网上找了很长时间,也没找到 解决的办法,自己慢慢的 ...
- 【Python】【元编程】【二】【描述符】
""" #描述符实例是托管类的类属性:此外,托管类还有自己实例的同名属性 #20.1.1 LineItem类第三版:一个简单的描述符#栗子20-1 dulkfood_v3 ...
- WCF配置后支持通过URL进行http方式调用
最近遇到一个小型项目,主要就是通过手机写入NFC信息,思考许久后决定就写一个简单的CS程序来搞定这个问题,可是当涉及到手机和PC通信的时候首先考虑到的就是IIS,同时因为数据库是SQLite,思前想后 ...
- 手动添加 launcher 到 Ubuntu Unity
本来,启动程序之后,在左边的launcher bar点右键,[Lock to Launcher]就可以的. 但是,有时候因为某种原因,需要手工添加. 这时候,就要参考下面的文章了 http://ask ...
- ubuntu10.04 交叉编译 aria2 总结
1) google之后,找到 这个 https://github.com/z24/pitv/tree/master/cross 的脚本, 觉得非常好. 于是准备用来进行编译 2) 安装交叉编译器 su ...
- "is not allowed to connect" mysql
好像过几次,安装mysql时,总会遇到这个问题. 每次都忘怎么解决. 这回写下来吧. 编辑 mysql数据库的 user表太麻烦了, 最简单的方法是加一个用户,以后就用这个用户登录 CREATE US ...