对于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练习的更多相关文章

  1. [C#基础]ref和out的区别

    在C#中通过使用方法来获取返回值时,通常只能得到一个返回值.因此,当一个方法需要返回多个值的时候,就需要用到ref和out,那么这两个方法区别在哪儿呢? MSDN:       ref 关键字使参数按 ...

  2. 10. react 基础 ref 的使用 及 React 16 的生命周期函数 及 生命周期函数使用场景

    一. ref 的使用 ( 直接获取 DOM 元素 ) 在 input 标签上 可以使用 ref 属性 获取当前DOM节点 eg: import React , { Component, Fragmen ...

  3. [C#基础]ref和out的使用

    在C#中如果需要把值类型转换成引用类型传递其他方法中并使其原来值发生改变,使用 ref 和 out 转换成引用类型传递. 1. ref : 使用ref之前需要定义变量并初始化(必须初始) class ...

  4. C#基础-ref、out

    1.默认情况下,C#假定所有的方法参数传递都是传值的. 如下面的方法: public static void Main(string[] args) { int val = 5; //调用AddVal ...

  5. C#基础--Ref与Out区别

    两者都是按地址传递的,使用后都将改变原来参数的数值. class Program { static void Main(string[] args) { int num = 1; Method(ref ...

  6. vue基础 ref的作用

    1.  ref 获取dom元素,除了能获取dom元素也能获取组件dom,   组件通信:        在父组件中直接调用ref定义的组件的数据或者方法 <div id="app&qu ...

  7. [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 ...

  8. Python :数据结构

    LearnPython :数据结构 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .labe ...

  9. Java.Annotations

    Annotation 0. Annotation Tricks http://developer.android.com/reference/java/lang/annotation/Annotati ...

随机推荐

  1. 托管C++调用C#

    拿到了一个第三方demo,有dll,有.cpp..h,打开解决方案,如下图: 网上资料貌似很少,根据猜测: 这是使用托管C++来调用C#的方式. 过程: 1.先使用C#代码实现界面和功能,其实就是一个 ...

  2. tensorflow的写诗代码分析【转】

    本文转载自:https://dongzhixiao.github.io/2018/07/21/so-hot/ 今天周六,早晨出门吃饭,全身汗湿透.天气真的是太热了!我决定一天不出门,在屋子里面休息! ...

  3. linux下递归列出目录下的所有文件名(不包括目录)

    1.linux下递归列出目录下的所有文件名(不包括目录) ls -lR |grep -v ^d|awk '{print $9}'2.linux下递归列出目录下的所有文件名(不包括目录),并且去掉空行 ...

  4. linux提示usb_serial_generic_write_bulk_callback - urb stoped: -32

    1.环境: 上位机:ubuntu16.04 Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_ ...

  5. POJ 1236 Network of Schools(tarjan)题解

    题意:一个有向图.第一问:最少给几个点信息能让所有点都收到信息.第二问:最少加几个边能实现在任意点放信息就能传遍所有点 思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度 tarjan算法:问 ...

  6. 第五章 消息摘要算法--MAC

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第6章“验证数据完整性--消息摘要算法” 5.1.mac(又称为Hmac) 原理:在md与sha系列算法的基础上加入了密钥,是 ...

  7. Codeforces Round #429 (Div. 2)

    A. Generous Kefa   One day Kefa found n baloons. For convenience, we denote color of i-th baloon as  ...

  8. springmvc基础知识及注解

    SpringMVC 1.概念 Spring的MVC框架是一个基于DispatcherServlet的MVC框架,主要由DispatcherServlet.处理器映射.处理器.视图解析器.视图组成.每一 ...

  9. 【Coursera】Sixth Week(1)

    Transport Layer 在学习完 Link Layer(Ethernet),Internetwork Layer(IP)之后,我们来到了TCP/IP协议簇的上半部分. Review:Magic ...

  10. LA 4254 处理器(二分+贪心)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...