C # Learning Diary---data type

Source: Internet
Author: User
C # Data types

The C # language is a strongly typed language that must have a type in the variables, expressions, values, etc. used in the program. For each amount of information stored in the program, we must declare his data type in order for the compiler to allocate memory space for him. The data types of C # can be divided into two parts: value types and reference types (2 commonly used String and Object).

Because of the characteristics of C # strongly typed, a data type in C # is a class. For example integer int his real name is System.Int32 ();


As we can see from the above table, the primitive types map directly to the types in the base class library, so the two are interchangeable, that is, we define a variable of type int,


int x = 12; (found to be similar to C + + definition data types)

Can be written like this: System.Int32 x = new System.Int32 (12);

The scope of the different type definitions is also different:

The float data type is used for smaller floating-point numbers because it requires less precision.

The double data type is larger than the float data type and provides one-fold greater precision (15-bit). In the C # compiler general, the default decimal type is double type,

If you want to specify that the value is float, you can add the character F (or F) after it, such as:

         float F = 12.3F;


The decimal type is specifically used for financial calculations, with high precision (28-bit).

To specify a number as a decimal type, you can add a character m or (m) after the number, such as:

    Decimal d=12.30m;

C # variable name naming rules:

1), consisting of letters, numbers, or underscores "_"

2), must start with a "letter" or an underscore "_" and cannot begin with a number

3), cannot be a keyword in C # such as: int, string, bool, Main, class, etc.

4), case-sensitive, such as: lowercase A and uppercase A is two variables


Here is an example, ask the user to enter the name of the age and height wage gender and then output "My name is: xx, sex is: xx, xx years old, height is: xx, wages are: xx"

Using System;  Using System.Collections.Generic;  Using System.Linq;    Using System.Text; Namespace Example {class Program {static void Main (string[] args) {string Nam  E   Defines the name char sex;   Define gender uint age;  Age (age cannot be negative) double hight; Height decimal salary;//Salary Console.WriteLine ("What's your name?")                 ");   Name = Console.ReadLine ();              Enter the name Console.WriteLine ("Your Gender is:"); Sex = Console.readkey ().  KeyChar;   Write sex Console.WriteLine (); Wrap Console.WriteLine ("How old are You?")              "); Age =uint.  Parse (Console.ReadLine ()); Write Age (UINT.                Parse () is the type of string to be read into the UINT type, ReadLine ()//The return value is String type) Console.WriteLine ("How Tall?")              "); Hight = Double.  Parse (Console.ReadLine ()); Write height Console.WriteLine ("How much is the salary?")              "); Salary = Decimal.  Parse (Console.ReadLine ());       Write the salary Console.WriteLine ("My name {0}, is a {1} child, this year {2} years old, my height is {3} meters, the salary has {4} yuan", name,sex,age,hight,salary); Output Information}}}

The resulting results are:

Alas! Again this black-box, dark ugly die, later learned WinForm should be better!

Based on the input and output in the 02 journal, a new output skill was obtained through the lookup, with {0},{1},{2} in the last line of code ... There can be many, this pair of "{}" is called a placeholder,,, (the following is my personal understanding) is that we dig in the program output of one after another pit, and then the first parameter ("" "in the content is a string parameter, parameters and parameters with a comma", "separated" parameters) after the order to fill in (A little vulgar).

In the spirit of the exploration of learning, I have explored him again; when we use placeholders, we must follow {0},{1},{2} ... This order? I try to change it or the above code, and the subsequent order is changed to {0},{3},{1},{2},{10}. Compile without the wrong, but run the output when printed, may be that 10 of the problem, the original can not jump digging pits, then {0},{3},{1},{2},{4}, this time did not jump, exchange the order, compile yes can run, but the result is this:

The total loss of common sense has turned out to be messy. After careful observation, it is found that the 0,1,2,3,4 is the serial number of the following parameters, {} Who is the number of the call.

In the spirit of daily provinces studious I have doubts, in 02 diary I output AAA and BBB when I used the double quotation mark "" to denote the string, but if I want to output double quotation marks what to do? Think for a long time, go online to check the next just know can use \ Add symbol can output, so I wrote a shameless program:

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Example  {      class program      {          static void Main (string[] args)          {                         Console.WriteLine (" The goddess said to me: ' I love you!!! \"");            }      }  }

The result is:

(Haha, shameless) anyway, the double quotation marks are the successful output.

In the spirit of doing a courageous exploration of the people, I found: the output double quotation marks when I use \ to not let him escape, if I want to output \ What to do?

May be in front of a \ Try the next yes \ \ can output \ So to output \ \ must write \\\ and so feel very troublesome, and then find a once and for all

The method "@",,, Console.WriteLine (@ "\\\");

Result output \\\ unchanged

The above is the C # learning Diary---Data types of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.