Home >Backend Development >C#.Net Tutorial >C# Learning Diary 03---Data Type

C# Learning Diary 03---Data Type

黄舟
黄舟Original
2017-01-20 13:22:431287browse

C#Data type

C# language is a strongly typed language. Variables, expressions, values, etc. used in the program must have types. For every amount used to save information in the program, we must declare its data type when using it so that the compiler can allocate memory space for it. C#'s data types can be divided into two parts: value types and reference types (the two commonly used ones are String and Object).

Due to the strong type characteristics of C#, a data type in C# is also a class. For example, the real name of the integer int is System.Int32();

C# Learning Diary 03---Data Type

## We can see from the above table that the basic types are directly mapped to the base class library type, so the two are interchangeable, that is to say, we define a variable of type int,


## The data type is similar)

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

The scope of different type definitions is also different:

C# Learning Diary 03---Data Type 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 twice the precision (15 bits). In the C# compiler, the default decimal type is generally double type.

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

         float f = 12.3F;


The decimal type is specially used for financial calculations, with high precision (28 bits).

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

    decimal d=12.30M;

C# Variable name naming rules:


1), composed of letters, numbers or underscore "_"

2), must start with "letters" or underscore "_", and cannot start with numbers

                                                                                                                                                                                                                      -          .

## Let’s write an example below, requiring the user to input name, age, height, salary and gender and then output “My name is: XX, gender is: XX, I am XX years old this year, and my height is It’s: XX, the salary is: XX”

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Example  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            String name;  //定义姓名为name  
            char sex;   //定义性别   
            uint age;   //年龄(年龄不能为负数)  
            double hight;  //身高  
            decimal salary;//工资  
                Console.WriteLine("你叫什么名字啊?");  
               name = Console.ReadLine();   //输入姓名  
  
             Console.WriteLine("你的性别是:");  
            sex = Console.ReadKey().KeyChar;  //写入性别  
            Console.WriteLine();   //换行  
  
            Console.WriteLine("你多大了?");  
            age =uint.Parse(Console.ReadLine());  //写入年龄(uint.Parse()是将读入的String类型转为uint类型,
            ReadLine()                                                    //的返回值是String 类型)  
  
            Console.WriteLine("多高啊?");  
            hight = double.Parse(Console.ReadLine());  //写入身高  
  
            Console.WriteLine("工资多少呀?");  
            salary = decimal.Parse(Console.ReadLine());  //写入工资  
  
            Console.WriteLine("我叫{0},是个{1}孩子,今年{2}岁了,我的身高是{3}米,工资有{4}元",name,sex,age,hight,salary);       //输出信息  
  
  
        }  
    }  
}

The result is:

## Alas! It’s this kind of black frame again, black and ugly. , it should be better if you learn Winform in the future!

Based on the input and output in the 02 diary, I got a new output skill through checking. In the last line of code above, there is {0}, {1}, {2}. . . There can be many, this pair of "{}" is called placeholder,,, (the following is my personal understanding) are the pits we dig one after another when the program outputs, and then use the first parameter ("" The content is a String type parameter, separated by commas "," between parameters.) The following parameters must be filled in in order (a bit cheesy).

C# Learning Diary 03---Data Type In the spirit of endless learning and exploration, I explored it again; When we use placeholders, we must follow {0}, {1}, {2}... ...In this order? I tried to change the above code, and changed the following order to {0}, {3}, {1}, {2}, {10}. There are no errors in compilation but the output cannot be printed when running. It may be the problem with 10. It turns out that you can't jump to dig holes, so it is like this {0}, {3}, {1}, {2}, {4} , this time it didn’t jump, I exchanged the order, the compilation was correct and it ran, but the result was this:

I completely lost my sense and the result was messy. After careful observation, I found that 0, 1, 2, 3, and 4 are actually the serial numbers for the subsequent parameters. Whoever has the serial number in {} will be called.

In the spirit of learning and self-examination three times a day, I have doubts again. When I output aaa and bbb in the 02 diary, I used double quotes "" to represent strings, but if I want to output double What should I do with quotation marks? After thinking about it for a long time, I checked online and found out that I can use \ plus symbols to 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("女神对我说:\"I Love You!!!\"");  
  
        }  
    }  
}
C# Learning Diary 03---Data TypeThe result is:

(Haha, no shame) In short, the double quotes were successfully output

In order to be a person who has the courage to explore, I also discovered: When outputting double quotes, I use \ to prevent them from escaping. What should I do if I want to output \?

Maybe add a \ in front of \. I tried it and it’s correct. \\ can output \, so to output \\, you have to write \\\ and so on. It feels quite troublesome, so I found another one that can be used once and for all.

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

The result output \\\ remains unchanged

The above is the C# learning diary 03---Data type content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
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