Summary: C # variables, placeholders and other related knowledge

Source: Internet
Author: User

New Year delay a lot of time, a long period of not seriously sit down to study, the new Year is almost finished, and began to formally study, according to the video teaching study, with a day of time, learning the next simple variable and its related input and output and application, learned several basic types:

int (integer) char (character type) string (string type) double (double-precision floating-point number) Decimal (currency value type) float (floating-point).

In the main method, duplicate declaration variables are not allowed, but they can be assigned repeatedly, and the value of the original variable is replaced with the new assigned value after the duplicate assignment.

In C #, there are two meanings of "+";

1. The value symbol, when the + left side as long as there is a character or string type, use "+" to connect the left and right side of the data.

2. The plus sign in mathematics, which participates in the operation of the data of the character type, represents the mathematical addition operation.

The assignment operator = (not the equals symbol in mathematics) is the lowest operational level in C #, which is executed at the end.

Two. Placeholder

First {0}

A second {1}

A third {2}

.......

For 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 variable with the associated value {class Program {            static void Main (string[] args) {string name;            Name = "Zhang San";            int age = 28; Age = 18;            Repeats the value of the assignment variable age.            Decimal pay = 7600.33m;            Console.Write ("I call" +name);            Console.Write (", this year" +age+ "years old,");            Console.Write ("My salary is +pay+" yuan.);                        Console.WriteLine ("I Call" +name+ ", this year" +age+ "years old," + "My salary is" +pay+ "Yuan."); Console.WriteLine ("My name {0}, this year {1} years old, my salary is {2} yuan.", name, age, pay);//{0}{1}{2} represents a placeholder.                       Placeholders can be reused and can be omitted. Console.WriteLine ("I Call" +name, "this year" +age+ "years old.");            /comma is the first parameter before the console outputs a comma before the first parameter. Console.WriteLine ("{0} I call" + name, "This year" + Age + "years old."); /{0} "This year" + Age + "years old."                                   The variable that replaces the preceding placeholder.            int a = 1;//is the same as a numeric type with "+" for mathematical addition.  String a = "1";              The use of a value symbol differs between the left and right side as long as there is a character or string type with a "+" is the associated value symbol.     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 = ' male ';            int age =;            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 = 3799m;            String weight = "0.3kg";//double weight = 0.3;            Console.WriteLine ("My Phone brand is {0}, the model number is {1}, the mobile phone price is {2} yuan, the weight is {3}", pho,type,money,weight);            Console.readkey ();}}}    

Console.ReadLine (); For receiving user input data, you need to define a variable of string type (string) to store the user's 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 ("Enter the front of this sentence");            Console.WriteLine ("Please enter a sentence here!") ");            string input = Console.ReadLine ();            Console.WriteLine ("Enter the back of this sentence");            Console.readkey ();}}}    

Three. Exchanging variable values

To exchange the values of two variables, you need to use the third variable to do so.

int a =5,b=10;

int C;

c = b;

b = A;

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 swap variable {    class program    {        static void Main (string[] args)        {   //the algorithm for exchanging two variables requires a third variable to be supported.            int a = 5;            int B = Ten;            int C;            c = A;            A = b;            b = C;            Console.WriteLine ("A={0} B={1}", A, b);            Console.WriteLine ("A={0} B={1}", b,a);//does not exchange values of two variables            console.readkey ();}        }    

Summary: C # variables, placeholders and other related knowledge

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.