C # Getting Started classic notes more about _CH05 variables

Source: Internet
Author: User

More about Chapter 05 variables

5.1 Type conversions

1. Regardless of the type, all data is a series of bits, that is, a set of 0 and 1. The meaning of a variable is conveyed by interpreting the data. The simplest example is a char type, which represents one character in a Unicode character set with a number. In fact, this number is the same as ushort storage----They are the numbers that are stored between 0~65535.

2. In general, different types of variables use different patterns to express data. This means that even if a series of bits can be moved from one type of variable to another (perhaps they occupy the same storage space, perhaps the target type has enough storage space to contain all the source data bits), the result may be different from what is expected. This is not a one-to-two mapping of data bits from a variable to another variable, but rather a conversion of the data.

5.1.1-Implicit conversion

1.bool and string do not have an implicit conversion

2. Rules for implicit conversions: any type A, as long as the range of values is fully contained within the range of the value of type B, it can be implicitly converted to type B.

5.1.2 Display Conversion

1. Keywords checked and unchecked

5.1.3 Display conversion using the Convert command

Convert.ToInt32 (Val)

The name of the conversion is slightly different from the C # type name, for example, to convert to int, use Convert.ToInt32 (). This is because these commands come from the System namespace of the. NET Framework, not the C # itself. This allows them to be used in other. NET compatible languages except C #.

5.2 Complex variable types

5.2.1 Enumeration

Sometimes you want a variable to extract a value from a fixed set, you can use an enumeration type.

1. Defining enumerations

Enumerations can be defined by using the enum keyword:

Enum Typename:underluingtype

{

Values1,

Values2,

......

Valuesn

}

Then declare this new type of variable:

TypeName VarName;

and assign values:

Varname=typename.value;

2. Enumerations are stored using a basic type (underlying). By default, the type is int, which can be byte,short,ushort,int,uint,long and ulong. So enumerations and arrays have only one type of data.

3. By default, each will be automatically assigned to the corresponding base type value according to the defined order (starting at 0). This means that the value of value1 is 0,value2 is 2 .... You can override this assignment procedure by using the = operator.

4. You can also use one value as the underlying value of another enumeration to specify the same value for multiple enumerations. Any value that does not have an assignment automatically gets an initial value, and the value used here is a sequence that is higher than the last explicitly declared value.

5. Convert enumerations to other data types?

6. Using the typeof (<var>) operator, you can get the operand type.

5.2.2 Structure

A structure is a data structure that consists of several data, which can have different types.

1. Defining the structure

Use the struct keyword to define

struct <typeName>

{

<menberDeclarations>

}

The <memberDeclarations> section contains the definition of a variable (a data member of a struct), in the same format as usual, and each member's declaration takes the following form:

<accessibility> <type> <name>;

Such as:

struct MYSTRUCT

{

PUBILC int x;

Pubilc double y;

}

Once you have defined this struct type, you can define a new type of variable to use this structure:

MyStruct mystr;

You can also access the data members in this combined variable by a period character:

mystr.x=2;

mystr.y=2.3;

5.2.3 Array

All of the preceding types have one thing in common: they all store only one value (the structure stores a set of values). Sometimes, you need to store a lot of data, 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 that are stored in variables of the array type. An array has a primitive type, and each element of the array is of this type.

1. Declaring an array:

<basetype>[] <name>;

Where <baseType> can be any variable type, including the enumeration and struct types described earlier in this chapter.

There are two ways of initializing an array.

1) The full contents of the array can be specified literally. You need to provide a comma-separated list of element values, which are enclosed in curly braces such as:

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

2) You can also specify the size of the array, and then use the New keyword to initialize the array elements. You need to use the following syntax:

Int[] myintarray= new int[5];

Notice

This initializes the array with the keyword new and defines its size with a constant. This way, all array elements are given the same default value, and the default value is 0 for numeric types. You can also initialize with a very quantitative amount, such as:

int[] myintarray=new int [arraySize];

3) In addition, you can also use the combination of these two initialization methods:

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

Using this method, the size of the array 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 with the const keyword such as:

const int arraysize=5;

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

2.foreach Cycle

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

{

can use <name> for each Elemet

}

This loop iterates through each element, placing each element in the variable <name> in turn, without the risk of access being illegal. The main difference between using this method and the standard for loop is that the Foreach loop has read-only access to the array contents, so you cannot change the value of any element.

3. Multidimensional arrays

1) Declaration of two-bit arrays:

<basetype>[,] <name>;

Multi-dimensional arrays require only more commas:

<basetype>[,,,] <name>;

2) Declaration and initialization

double[,] hillheight=new double[3,4];

You can also use literal values for initial assignment, where you use nested curly braces separated by commas, such as:

double[,] hillheight={{1,1,1,},{2,2,2,2},{3,3,3,3}};

4. Array of arrays (variable-length arrays)

You can use variable-length arrays, where each row has a different number of elements. To do this, you need an array in which each element is another array. You can also have an array of arrays, or more complex arrays. Note, however, that these arrays must have the same base type.

1) Declare an array of arrays (variable-length arrays) whose syntax is to specify multiple square brackets in the declaration of an array, for example:

Int[][] Jaggedintarray;

2) Initialization

There are two ways: You can initialize an array that contains other arrays (called sub-arrays) and initialize the Subarray in turn:

Jaggedintarray=new int[2][];

Jaggedintarray[0]=new Int[3];

Jaggedintarray[1]=new Int[4];

You can also use an improved form of the above literal value assignment:

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

It can also be simplified to put the initialization and declaration of an array on the same line, such as:

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

3) When using a foreach iteration, you typically need to nest the loop, looping the array itself and each sub-array. Because the array jaggedintarray contains the int[] element, not the int element.

5.3 Handling of strings

A 1.string type variable can be thought of as a read-only group of char variables, such as:

String mystring= "A string";

Char mychar=mystring[4];

2. However, you cannot assign values to individual character variables in this way. In order to obtain a writable char array, you can use the following code, which uses the ToCharArray () command of the array variable:

String mystring= "A string";

Char[] Mychars=mystring.tochatarray ();

The char array can then be processed in a standard manner. You can also use strings in a foreach loop.

3.mystring.length;

4.mystring.tolower (); Mystring.toupper ();

5.<string>. Trim ()

1) <string> can be used;. Trim () Delete the input content in front of <string>. TrimStart () and the space behind the <string>. TrimEnd ().

2) You can also use this command to delete a specified character, as long as you specify the characters in a char array, such as:

Char[] trimchars={', ' e ', ' s '};

String Userresponse=console.readline ();

Userresponse=userresponse.trim (TrimChars);

if (userresponse== "Y")

{

Act on Response.
}

6.<string>. PadLeft () and <string>. PadRight ()

1) You can add spaces 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, which requires a char, such as:

Mystring= "Aligned";

Mystring=mystring.padleft (10, '-'); This will add 3 '-' at the beginning of the mystring.

7.<string>. Split ()

1) You can convert a string to a string array, separating its specified position. These locations take the form of a char array. Such as:

String myString = "This is a test."

Char[] separator={'};

String[] Mywords=mystring.split (separator);

Notice

When you use Split (), the delimiter is removed.

C # Getting Started classic notes more about _CH05 variables

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.