An algorithm for getting started with C # basics-code examples for swapping

Source: Internet
Author: User
This paper mainly introduces the algorithm in C #: The related knowledge of exchange, it has good reference value. Let's take a look at the little series.

Swapping the values of 2 variables is like swapping drinks from two cups: a glass full of milk and a cup full of coffee, how can you exchange it?

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


String a= "vibration";//the first variable string b= "Wenfeng";//second variable string temp;//intermediate variable//First step: Assign variable A to the middle variable temp=a;//as milk pour empty cup//second step: Assign variable B to variable aa=b ;//As coffee into the Milk Cup//Third step: Assign the middle variable to the variable bb=temp;//as the milk in the empty cup into the coffee cup//At this time the exchange is complete, variable a stores "Wenfeng", b Storage "vibration just"

The process of solving practical problems is called " algorithm ", and switching is one of the most common algorithms.

Another example:

Namespace test{  class program  {    static void Main (string[] args)    {      string child = "Beauty";//Boy name      String girl = "Wei Qiang";//girl name      string temp;//middle variable      temp = boy;//assigns the boy's name to temp      boy = girl;//assigns the girl's name to the boys      Girl = temp;//assigns the name in the temp to the girl      Console.WriteLine ("The boy is called" +boy+ "the girl is called" +girl);}}  

The result of the operation is:

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.