Use the out keyword in C # And the World Cup

Source: Internet
Author: User

This year's World Cup looks amazing and is full of stories and drama. No matter whether you usually play or not, or whether you understand the ball, you can get some emotional relief from it. The World Cup seems to surpass the football itself and become the "sum of emotions" of people all over the world. A single game affects the hearts of countless people. The ups and downs of the competition within the stadium and the joys and sorrows of fans outside the stadium.

 

Our national team seems to be moving away from the World Cup stage. During the World Cup, when the number of people in the field was dominant, the team lost to the unnamed Mali team, which was a big bang. So I sighed: What is the day of Chinese football? Several players agreed that we could choose not to watch the Chinese team! Although this is the case, there are always some people who are looking forward to the day when Chinese football gets better ......

 

When the playground, the park, the street, and the field are full of kids playing football, when there are enough venues around us to play football, when we are a father, we take our children to the stadium. When we have a large number of professional young coaches to teach our children to play football, when schools, clubs, and Chinese football associations attach importance to youth training and are willing to invest, when we are in a transitional period and pushed forward by the times, we realize that material and money are not all, football can bring happiness to our hearts ...... By that time ...... I ran the question. By the way, this article is intended to use the out keyword of C #, which reminds me that this is quite similar to the football match's selection of "the best player in this field". I will follow this idea.


The game is completed by players on the court. An IsBestPlayer attribute of the Bool type is also required to set whether the player is the best player:

public class Player    {        public int Id { get; set; }        public string Name { get; set; }        public string Position { get; set; }         public bool IsBestPlayer { get; set; }    }

The ScoreCount attribute of a player belonging to a team. The int type is used to record the number of players in the team. It is used to determine which team wins the game:

public class Team    {        public Team()        {            Players = new List<Player>();        }              public int Id { get; set; }        public string Name { get; set; }        public int ScoreCount { get; set; }        public List<Player>  Players { get; set; }    }

We need a way to determine which Team won the game, and select the best player in the game, and take the best player as the output parameter:

Static void WhoWinWorldCup (Team a, Team B, out string bestPlayer) {if (. scoreCount> B. scoreCount) {Console. writeLine ("Congratulations {0} won this World Cup ~~ ", A. Name);} else {Console. WriteLine (" Congratulations {0} won this World Cup ~~ ", B. Name);} bestPlayer = LookForBestPlayer (a, B );}

The LookForBestPlayer () method is used to obtain the best players in two teams:

Static string LookForBestPlayer (Team a, Team B) {string result = string. empty; // merge the players of Team B into Team. players. addRange (B. players); foreach (var player in. players) {if (player. isBestPlayer = false) {continue;} else {result = player. name; break ;}} return result ;}

Finally, on the client side, he Wei, the CCTV Football commentator, commented on the game for us. He Wei's explanation is accurate, professional, and even contains a few philosophical tastes:

Static void Main (string [] args) {Console. WriteLine ("He Wei, CCTV Football commentator: Welcome to the finals of this World Cup ~~ "); Console. writeLine ("CCTV Football commentator He Wei: the two teams in the finals are:"); Team brazil = new Team () {Id = 1, Name = "brazil ", players = new List <Player> () {new Player () {Id = 1, Name = "neymal", Position = "Forward"}, new Player () {Id = 2, Name = "Alves", Position = "" }}; Team germany = new Team () {Id = 2, Name = "germany ", players = new List <Player> () {new Player () {Id = 3, Name = "Zyl", Position = "Forward"}, new Player () {Id = 4, Name = "Ram ", Position =" Defender "}}; Console. writeLine ("{0} from South America vs. {1} from Europe", brazil. name, germany. name); Console. writeLine ~~ "); Germany. players [0]. isBestPlayer = true; germany. scoreCount = 1; brazil. scoreCount = 0; string best = string. empty; WhoWinWorldCup (brazil, germany, out best); Console. writeLine ("the best players in this game are: {0}", best); Console. writeLine (); Console. writeLine ("He Wei, CCTV Football commentator: It takes 90 minutes to reach the hearts of the people. After this competition, there will always be a team fans love to leave, and this competition itself will become an eternal fortune in our memory. When we get old and hold our grandson by the fireplace, we will certainly talk to them about the battle of bud tonight in 2014. "); Console. ReadKey ();}

Run:

 

Out and ref:
● The Same Thing Is That The passed references
● The difference is that: Before using ref, you need to assign the initial value. You can assign the initial value or not assign the out value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.