C # Learning Notes (implicit and explicit conversions, enum types, struct types, array types, and strings)

Source: Internet
Author: User

1 Type conversions

1.1 Implicit conversions : Conversions from Type A to type B can be done in all cases, and the rules for performing the transformation are simple enough for the compiler to perform the conversion.

UShort Destinationvar;

Char Sourcevar = ' a ';

Destinationvar = Sourcevar;

Console.wirteline ("Souecevar val:{0}", Sourcevar);

Console.wirteline ("Destination val:{0}", Destinationvar);

Results of the output:

Sourcevar val:a

Destination val:97

Real-time two variables store the same information, but different types interpret them in a different way.

The compiler allows the conversion of the numeric value of an implicit conversion to the following table:

Type can be safely converted to
Byte Short,ushort,int,uint,long,ulong,double,decimal
SByte Short,int,long,float,double,decimal
Short Int,long,float,double,decimal
UShort Int,uint,long,ulong,float,double,decimal
Int Long,float,double,decimal
UInt Long,ulong.float,double,decimal
Long Float,double,decimal
ULong Float,double,decimal
Float Double
Char Ushort,int,uint,long,ulong,float,double,decimal

1.2 Explicit Conversions : Conversions from Type A to type B can only be done in some cases, the rules of the conversion are complex, and some type of processing should be performed.

BYTE Destinationvar;

Short Sourcevar = 7;

Destinationvar = (byte) Sourcevar;

1.2.1 Using the Convert command for an explicit conversion

Command Results
Convert.toboolean (Val) Val conversion to BOOL
Convert.tobyte (Val) Val conversion to Byte
Convert.tochar (Val) Val conversion to Char
Convert.todecimal (Val) Val conversion to decimal
Convert.todouble (Val) Val converts to Double
Convert.ToInt16 (Val) Val conversion to Short
Convert.ToInt32 (Val) Val conversion to int
Convert.toint64 (Val) Val converts to Long
Convert.tosbyte (Val) Val conversion to SByte
Convert.tosingle (Val) Val conversion to float
Convert.ToString (Val) Val converted to string
Convert.touint16 (Val) Val conversion to UShort
Convert.touint32 (Val) Val conversion to UINT
Convert.touint64 (Val) Val conversion to ULONG

2 enum types

Keyword ENMU

Enmu<typename>

{

<value1>,

<value2>,

<value3>,

...

<valueN>

}

Then declare this new type

<typeName><varName>;

and assign values:

<varName>=<typeName><value>;

Note: By default, the first variable in the enumeration is assigned a value of 1, and the following value automatically adds 1

3 Structure Type

Keyword struct

struct <typeName>

{

<memberDeclarations>

}

The <menberDeclarations> section contains declarations of variables, called data members of structs, in the same format as usual. Each member's declaration takes the following format:

<accessibility><type><name>;

For the code that invokes the structure to access the data members of the structure, you can use the keyword public for <accessibility>, for example:

struct route

{

public orientation direction;

public double distance;

}

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

Route Myroute;

The data members in this composite variable are then accessed by the period character:

Myroute.direction = Orientation.north;

Myroute.distance = 10;

4 arrays

All of the preceding types have one thing in common: they can only store one value (the structure stores a set of values). Sometimes, you need to store a lot of data, which can cause inconvenience. Sometimes you need to store several types of the same value at the same time, instead of using a different variable for each value.

4.1 Declaring an array:

<baseType>[]<name>;

For example: int[] Myintarray;

MYINTARRAY[10] = 5;

Or: int[] Myintarray = new Int[]arraysize;

Or: int[] Myintarray = new int[5]{1,2,3,4,5}; The array size must match the number of elements

4.2 Foreach Loop

The Foreach loop can use a simple syntax to locate each element in the array:
foreach (<baseType> (name) in <array>)

{

Can usr <name> for each element

}

4.3 Multi-dimensional arrays

Two-dimensional array declaration

<basetype>[,] <name>;

Multidimensional Array Declaration

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

5 Handling of strings

A string type variable can be viewed as a read-only group of char variables. This allows you to access each character using the following syntax:

String myString = "A string";

Char MyChar = mystring[1];

To use a string in a Foreach loop:

foreach (char character in mystring)

{

Console.wirteline ("{0}", character);

}

C # Learning Notes (implicit and explicit conversions, enum types, struct types, array types, and strings)

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.