Basic Introduction to C# Algorithm-Exchange Code Example

黄舟
Release: 2017-03-17 11:26:17
Original
1706 people have browsed it

This article mainly introduces the algorithm in C#: exchange-related knowledge, which has a good reference value. Let’s take a look with the editor below

Exchanging the values ​​​​of two variables is like exchanging the drinks in two cups: a cup full of milk and a cup full of coffee. How can they be interchanged?

The same goes for the exchange of variables. The exchange of two variables also requires an "empty cup", which is intermediate variable:


string a="振刚";//第一个变量
string b="文峰";//第二个变量
string temp;//中间变量
//第一步:将变量a赋值给中间变量
temp=a;//如同牛奶倒入空杯
//第二步:将变量b赋值给变量a
a=b;//如同咖啡倒入牛奶杯
//第三步:将中间变量赋值给变量b
b=temp;//如同空杯中的牛奶倒入咖啡杯
//此时交换完成,变量a存储了“文峰”,b存储了“振刚”
Copy after login

This kind of step to solve practical problems is called " Algorithm", exchange is the most commonly used algorithm.

Another example:

namespace Test
{
  class Program
  {
    static void Main(string[] args)
    {
      string boy = "秀丽";//男孩名字
      string girl = "伟强";//女孩名字
      string temp;//中间变量
      temp = boy;//把男孩的名字赋值给temp
      boy = girl;//把女孩的名字赋值给男孩
      girl = temp;//把temp中的名字赋值给女孩
      Console.WriteLine("男孩叫"+boy+" 女孩叫"+girl);
    }
  }
}
Copy after login

The running result is:

The above is the detailed content of Basic Introduction to C# Algorithm-Exchange Code Example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!