C # getting started with the classic notes _ Ch05 variable

Source: Internet
Author: User

Chapter 05

5.1

1. No matter what type, all data is a series of BITs, that is, a group

2. Generally, different types of variables use different modes to express data. This means that even if a series of BITs can be moved from one type of variable to another type of variable (maybe they occupy the same storage space, maybe the target type has enough storage space to contain all the source data bits), and the result may be different from the Expected One. This is not a one-to-one ing between a data bit and another, but a conversion of data.

5.1.1

1. bool

2. implicit conversion rules: Any Type

5.1.2

1. Keywords

5.1.3

Convert. ToInt32

The conversion name is slightly different from

 

5.2

5.2.1

You can use the enumeration type to extract values from a fixed set.

1. Define Enumeration

Enumeration can be used

Enum typeName: underluingType

{

Values1,

Values2,

......

ValuesN

}

Declare the new type of variable:

TypeName varName;

And assign values:

VarName = typeName. value;

2. Enumeration uses a basic type (

3. By default, each of them will only follow the defined order (from

4. You can also use one value as the base value of another enumeration and specify the same value for multiple enumerations. If no value is assigned, an initial value is automatically obtained. The value used here is a sequence larger than the value explicitly declared last time.

5. Convert enumeration to other data types?

6. Use

5.2.2

A structure is a data structure composed of several data types.

1. Define the structure

Use

Struct <typeName>

{

<MenberDeclarations>

}

<MemberDeclarations>

<Accessibility> <type> <name>;

For example:

Struct myStruct

{

Pubilc int x;

Pubilc double y;

}

After defining the structure type, you can define the new type of variables to use this structure:

MyStruct myStr;

You can also access the data members in the composite variables through the periods:

MyStr. x = 2;

MyStr. y = 2.3;

 

5.2.3

All the preceding types have one thing in common: they store only one value (a group of values in the structure ). Sometimes, you need to store a lot of data, and sometimes you need to store several values of the same type at the same time, instead of using different variables for each value.

An array is a subscript list of variables stored in an array type variable. An array has a basic type. All elements of an array belong to this type.

1.

<BaseType> [] <name>;

Where

There are two methods to initialize an array.

1

Int [] myIntArray = {1, 4, 5, 7, 9 };

2

Int [] myIntArray = new int [5];

Notice

Keyword used here

Int [] myIntArray = new int [arraySize];

3) You can also use the combination of the two initialization methods:

Int [] myIntArray = new int [5] {1, 3, 5, 7, 9 };

In this way, the array size must match the number of elements.

If you use a variable to define its size, the variable must be a constant and must be used

Const int arraySize = 5;

Int [] myIntArray = new int [arraySize] {1, 3, 5, 7, 9 };

2. foreach

Foreach (<baseType> <name> in <array>)

{

// Can use <name> for each elemet

}

This loop iterates each element and places each element in the Variable

3. Multi-dimensional array

1

<BaseType> [,] <name>;

Multi-dimensional arrays only need more commas:

<BaseType> [,] <name>;

2) Declaration and initialization

Double [,] hillHeight = new double [3, 4];

You can also use the literal value for initial assignment. nested curly braces are separated by commas, for example:

Double [,] hillHeight = {, 1,}, {,}, {, 3 }};

4. array (variable-length array)

You can use a variable-length array, where each row has a different number of elements. Therefore, we need such an array, where each element is another array. You can also have arrays or more complex arrays. However, note that these arrays must have the same basic type.

1) declare an array (variable-length array). Its Syntax must specify multiple square brackets in the array declaration. For example:

Int [] [] jargedintarray;

2) initialization

You can initialize an array (sub-array) containing other arrays and initialize the sub-array in sequence:

Jargedintarray = new int [2] [];

Jargedintarray [0] = new int [3];

Jargedintarray [1] = new int [4];

You can also use an improved form of the aforementioned Literal Value assignment:

Jargedintarray = new int [3] [] {new int [] {1, 2, 3}, new int [] {1}, new int [] {1 }};

You can also simplify the initialization and declaration of arrays on the same row, for example:

Int [] [] jaggedIntArray = {new int [] {1, 2, 3}, new int [] {1}, new int [] {1, 2 }};

3) Use

5.3

1. string

String myString = "A string ";

Char myChar = myString [4];

2. However, you cannot assign values to each character variable in this way. To obtain a writable

String myString = "A string ";

Char [] myChars = myString. ToChatArray ();

Then we can use the standard method for processing.

3. myString. Length;

4. myString. ToLower (); myString. ToUpper ();

5. <string>. Trim ()

1

2

Char [] trimChars = {'', 'E','s '};

String userResponse = Console. ReadLine ();

UserResponse = userResponse. Trim (trimChars );

If (userResponse = "y ")

{

// Act on response.
}

6. <string>. PadLeft ()

1) spaces can be added to the left or right of the string.

MyString = "Aligned ";

MyString = myString. PadLeft (10 ,);

2) You can also add the specified character to the string.

MyString = "Aligned ";

MyString = myString. PadLeft (10 ,'-');//'-'.

7. <string>. Split ()

1) You can set

String myString = "This is a test ."

Char [] separator = {''};

String [] myWords = myString. Split (separator );

Notice:

In use

 

 

 

 

 

 

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.