(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 = 10; } public static void ChangePet(ref Pet p) { p = new Pet() { Age=20 }; } } 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 = 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