(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 ...
随机推荐
- C# 计算传入的时间距离今天的时间差
/// <summary> /// 计算传入的时间距离今天的时间差 /// </summary> /// <param name="dt">&l ...
- Pyinstaller 中 pandas出错问题的解决(详细)
环境配置 pip install pyinstaller pyinstaller中的参数 -F 表示生成单个可执行文件 -c 显示命令行窗口,一般一开始的时候使用,如果没有错误,那就使用-w命令 -w ...
- 【Coursera】History: Dawn of Electronic Computing学后小结
今天学习了Coursera上University of Michigan开的互联网的历史.技术和安全课程的FirstWeek内容. 先是吐槽下这个Coursera,认证非常麻烦,PC端需要摄像头拍照. ...
- 51nod 1412 AVL树的种类(经典dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 题意: 思路: 经典dp!!!可惜我想不到!! $dp[i][k] ...
- 【转】Windows Server 2008 R2怎样设置自动登陆
Windows Server 2008 R2是一款服务器操作系统,提升了虚拟化.系统管理弹性.网络存取方式,以及信息安全等领域的应用,Windows Server 2008 R2也是第一个只提供64位 ...
- Intellij IDEA 使用spring-boot-devtools无效解决办法一
Intellij IDEA 使用spring-boot-devtools maven依赖 ``` <dependency> <groupId>org.springframewo ...
- 如何 Graphics 对象设置背景色
用 Clear 方法可以轻松地给 Graphics 对象设置背景色. using (Bitmap bmp = new Bitmap(width, height)){ using (Graphic ...
- python里的apply,applymap和map的区别
apply,applymap和map的应用总结: apply 用在dataframe上,用于对row或者column进行计算: applymap 用于dataframe上,是元素级别的操作: map ...
- Python定位SVG元素
svgelementXpath = "//div[12]/*[name()='svg']/*[name()='g']/*[name()='g'][2]/*[name()='g'][1]/*[ ...
- VS2010_x86_编译错误
1.两个头文件 相互include 报出来的错误,没有直接说是 嵌套include,而是这个现象: error: C4430: 缺少类型说明符 - 假定为 int.注意: C++ 不支持默认 int ...