C # study notes [two] - Data Types

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/BreakingDawn0/article/details/102759719

2.1 Value Type

byte:

int:

long:

float:12.3f

doube:

2.2 non-numeric type

char:

bool:

   true,false

string:

   String processing

    Create a string string s = "www.devsiki.com";

    Get string length s.Length (Properties)

    Comparing strings as if s == "www.devsiki.com"

    Connecting string s = "http: //" + s;

    Using a similar syntax indexer to obtain a character string stringName [index] s [0] s [3]

    About string String: string data type string is actually create an immutable, once the object has been initialized to the string, the string can not be changed content, and the above example is actually created a new string, copy the contents of the old string to the new string. Then assign the new reference to the string object string. (Repeat to modify a given string, is inefficient)

StringBuilder:

    Create a StringBuilder object:

StringBuilder sb1 = new StringBuilder("Lemon");//利用构造函数创建StringBuilder

StringBuilder sb2 = new StringBuilder(30);//初始一个空的StringBuilder对象,占30个字符大小

StringBuilder sb3 = new StringBuilder("Lemon", 50);

    When we need to add strings frequent delete operations, more efficient use StringBuilder

2.3 literals

2.4 escape character

@ Special role of:

   @ String added before, does not recognize the escape character string (character as its output) (double quotes, in which case "" represents the output quotes ").

string str = @"Lemon\n""";

    The two rows are defined in a character string.

    He represents the path.

string path = @"C:\xxx\xxx\xxx.doc";

2.5 mathematical operators

    + : Numeric string and the result is a string. Digital and null characters are added into digital characters.

2.6 Assignment operators

2.7 Boolean operations

int score = 99;

bool res = score >= 90;

    The results return True or False

!,&,|,^:

   NOT, AND, OR, XOR

&&:

   Which are true about the formula, it is true (false & left, the right is not determined, false).

2.8 Type Conversion

Implicit conversion:

    Small type data assigned to the major types of automatic compiler always converted, otherwise an error is reported.

Explicit conversion:

   The display converter (casts) large type data assigned to a small type. Plus () Convert by methods described or,

2.9 enumerated types

    Enumerated type definition

    enum <typeName>{

                 <value1>,

                 <value2>,

                 <value3>,

                 ...

                 <ValueN>

    }

    Enum type declaration <typeName> <varName>;

    Enumeration type assignment <varName> = <typeName> <value>.;

    Can use <typeName> <varName> = <typeName> <value>.;

enum GameState
    {
        Pause,
        Failed,
        Success,
        Start
    }

GameState state = GameState.Start;
             if (state == GameState.Start)

             {
                 Console.WriteLine("游戏开始");
             }
             Console.ReadKey();

Enumeration type default integer, followed by 0,1, ......, generally do not modify the default value (= can be directly specified)

You can also modify the type of data storage.

enum GameState:byte

2.10 / structured body

Definition structure

struct <typeName>{

   <memberDeclarations>

}

<MemberDeclarations> is the structure members, each member of the following declarations

<type> <name>;

struct Position
{
    public float x;
    public float y;
    public float z;
}
    Position enemyPosition1;
    enemyPosition1.x = 66;

Enum type and structure: when a plurality of types of ranges, enumerated type defined ease of reading, the structure is a few          

A combination of types, can be regarded as a combination of several types of new type.

2.11 Array

2.11.1 declare an array of

    <baseType>[] <name>;

    Array is an indexed list of variables, this index is an integer index of the first entry is 0.

int[] scores;
scores = new int[10];
scores = new int[3] { 1, 2, 3 };
string[] name = new string[2] { "Lemon", "SuSu" };

2.11.2 iterate

for,while,foreach

for (int i = 0; i < scores.Length; i++){}
foreach (int temp in scores){} //foreach依次取到数组中的每个值,赋值给temp,然后执行循环体,直    //到达到数 组长度

 

Guess you like

Origin blog.csdn.net/BreakingDawn0/article/details/102759719