Summary: C # variables, placeholders, and other related knowledge,

Source: Internet
Author: User

Summary: C # variables, placeholders, and other related knowledge,

The New Year has been delayed for a lot of time. I haven't sat down to study seriously for a long time. The new year is almost over, and I have to start to study officially. It took me one day to learn by video, I learned simple variables and their related input, output, and applications, and learned several basic types:

Int (integer) char (numeric) string (string type) double (double-precision floating point number) decimal (currency value type) float (floating point number ).

In the Main method, repeated variable declaration is not allowed, but repeated values can be assigned. After repeated values are assigned, the original variable value is replaced with the newly assigned value.

1. in C #, "+" has two meanings;

1. Bind a value symbol. When either side of the plus or minus sign is a character or string type, use "+" to connect the data between the two sides.

2. the plus sign in mathematics is involved in the calculation of complex data, indicating the addition operation in mathematics.

The value assignment operator = (not the equal sign in mathematics) is the lowest calculation level in C # And is executed at the end.

2. placeholders

First {0}

Second {1}

Third {2}

.......

Example: Console. WriteLine ("name {0} gender {1} age {2}", name, sex, age );

Using System; using System. collections. generic; using System. linq; using System. text; namespace output variables and associated values {class Program {static void Main (string [] args) {string name; name = "Zhang San"; int age = 28; age = 18; // repeat the value of the variable age. Decimal pay = 7600.33 m; // Console. write ("My name is" + name); // Console. write (", this year" + age + "years old,"); // Console. write ("My salary is" + pay + "RMB. "); // Console. writeLine ("My name is" + name + ", this year" + age + "years old," + "My salary is" + pay + "RMB. "); Console. writeLine ("My name is {0}. I am {1} years old. My salary is {2} RMB. ", name, age, pay); // {0} {1} {2} represents a placeholder. Placeholders can be reused and can be omitted. Console. writeLine ("My name is" + name, "this year" + age + "years old. "); // The first parameter before the comma, And the console outputs the first parameter before the comma. Console. writeLine ("{0} My name is" + name, "this year" + age + "years old. "); // {0}" this year "+ age +" years old. "Replace the placeholder variable. Int a = 1; // The addition of the same numeric type is expressed by "+. // String a = "1"; the difference in usage of the Union value symbol. "+" indicates the Union value as long as either side has a character or string type. Int B = 2; Console. WriteLine (a + B); Console. WriteLine ("1 + 2"); Console. ReadKey ();}}}
Using System; using System. collections. generic; using System. linq; using System. text; namespace variable job {class Program {static void Main (string [] args) {string name = "Zhang San"; string Tel = "13111111111"; char sex = 'mal '; int age = 25; Console. writeLine ("{0}, {1}, {2}, {3}", name, Tel, sex, age); Console. readKey ();}}}
Using System; using System. collections. generic; using System. linq; using System. text; namespace variable job 4 {class Program {static void Main (string [] args) {string Pho = "SAMSUNG"; string type = "I9300"; decimal money = 3799 m; string weight = "0.3 kg"; // double weight = 0.3; Console. writeLine ("my mobile phone brand is {0}, model is {1}, mobile phone price is {2} yuan, weight is {3}", Pho, type, money, weight); Console. readKey ();}}}

 

Console. ReadLine (); used to receive user input data. You need to define a string variable to store user variables.

String input;

Input = Console. ReadLine ();

Equivalent to string input = Console. ReadLine ();

Using System; using System. collections. generic; using System. linq; using System. text; namespace user input {class Program {static void Main (string [] args) {// string input; Console. writeLine ("before entering this sentence"); Console. writeLine ("enter a sentence here! "); String input = Console. ReadLine (); Console. WriteLine (" after this sentence is entered "); Console. ReadKey ();}}}

3. Exchange variable values

To exchange the values of two variables, you must use the third variable.

Int a = 5, B = 10;

Int c;

C = B;

B =;

A = c;

Console. WriteLine ("a = {0} B = {1}", a, B );

Console. ReadKey ();

Using System; using System. collections. generic; using System. linq; using System. text; namespace exchange variable {class Program {static void Main (string [] args) {// Algorithm for exchanging two variables, the third variable needs to be introduced. Int a = 5; int B = 10; int c; c = a; a = B; B = c; Console. writeLine ("a = {0} B = {1}", a, B); Console. writeLine ("a = {0} B = {1}", B, a); // The Console does not exchange the values of the two variables. readKey ();}}}

 

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.