首页 / C# / (C#基础) ref 和out练习
(C#基础) ref 和out练习
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(C#基础) ref 和out练习,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3833字,纯文字阅读大概需要6分钟。
内容图文
对于C#中这两个关键字的用法,常常混淆,有点不清楚,今天又一次看到。遂把它们都记录下来,希望能有所用。这些都是他人写的,我只是搬过来一次,加深印象。
代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace dazilianxi.wenjian { public class MoTes:IEnumerable<SanWei> { privatereadonly List<SanWei> _motes; public MoTes() { _motes = new List<SanWei>(); } publicvoid 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 } //一定要灵活使用publicstruct SanWei { publicreadonlydouble _Xiong; publicreadonlydouble _Yao; publicreadonlydouble _Tun; public SanWei(double xiong,double yao,double tun) { this._Xiong = xiong; this._Yao = yao; this._Tun = tun; } } publicclass Pet { publicint Age { get; set; } publicstaticvoid ChageAge(Pet p) { p.Age = 10; } publicstaticvoid ChangePet(ref Pet p) { p = new Pet() { Age=20 }; } } publicclass Player { publicint Id { get; set; } publicstring Name{get;set;} publicstring Position { get; set; } publicbool IsbestPlayer { get; set; } } publicclass Team { public Team() { listPlay = new List<Player>(); } publicint Id { get; set; } publicstring Name { get; set; } publicint ScoreCount { get; set; } public List<Player> listPlay { get; set; } /* out和ref: ● 相同的地方在于:传递的引用 ● 不同之处在于:ref在使用之前需要赋上初值,out可以赋初值也可以不赋 *///out为了在方法内改变值,然后在外面调用,ref 是作为一个判断条件,在方法里用,也可以发生改变publicstaticvoid WhoWinWorldCup(Team a, Team b, outstring bestPlayer) { if(a.ScoreCount>b.ScoreCount) { Console.WriteLine("恭喜{0}对,赢得了这次比赛",a.Name); } else { Console.WriteLine("恭喜{0}对,赢得了这次比赛",a.Name); } bestPlayer=LookForBestPlayer(a,b); } publicstaticstring 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 = 1, Name = "巴西队", listPlay = new List<Player>() { new Player(){Id = 1, Name = "内马尔", Position = "前锋"}, new Player(){Id = 2, Name = "阿尔维斯", Position = "后卫"} } }; Team germany = new Team() { Id = 2, Name = "德国队", listPlay = new List<Player>() { new Player(){Id = 3, Name = "齐勒", Position = "前锋"}, new Player(){Id = 4, Name = "拉姆", Position = "后卫"} } }; Console.WriteLine("来自南美的{0}主场迎战来自欧洲的劲旅{1}", brazil.Name, germany.Name); Console.WriteLine("在比赛的89分钟,德国队前锋齐勒禁区外抽射死角,锁定胜局~~"); germany.listPlay[0].IsbestPlayer = true; germany.ScoreCount = 1; brazil.ScoreCount = 0; 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
原文:http://www.cnblogs.com/annabook/p/4973980.html
内容总结
以上是互联网集市为您收集整理的(C#基础) ref 和out练习全部内容,希望文章能够帮你解决(C#基础) ref 和out练习所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。