C # basic-variables and constants (2 ),

Source: Internet
Author: User

C # basic-variables and constants (2 ),

I. Enumeration type:

Enumeration is a unique value type used to declare a group of constants of the same nature.

1. Create a console application to determine the day of the week by enumeration:

1 class Program 2 {3 enum MyDate // use enum to create enumeration 4 {5 Sun = 0, // set the enumerated Value Name Sun, the enumerated value is 0 6 Mon = 1, // set the enumerated value Mon and the enumerated value 1 7 Tue = 2. // set the enumerated value Tue and the enumerated value 2 8 Wed = 3, // set the enumerated Value Name Wed, the enumerated value is 3 9 Thi = 4, // set the enumerated Value Name Thi, And the enumerated value is 410 Fri = 5, // set the enumerated value name to Fri and the enumerated value to 511 Sat = 6 // set the enumerated value to Sat And the enumerated value to 612} 13 14 static void Main (string [] args) 15 {16 int k = (int) DateTime. now. dayOfWeek; // obtain the return value representing the day of the week 17 switch (k) 18 {19 // if k is equal to the Sun enumerated value in the enumerated variable MyDate, the output is Sunday 20 case (int) MyDate. sun: Console. writeLine ("Today is Sunday"); break; 21 // if k is equal to the enumerated value of Mon in the enumerated variable MyDate, the output today is Monday 22 case (int) MyDate. mon: Console. writeLine ("Today is Monday"); break; 23 // if k is equal to the Tue enumerated value in the enumerated variable MyDate, It is output today is Tuesday 24 case (int) MyDate. tue: Console. writeLine ("Today is Tuesday"); break; 25 // if k is equal to the Wed enumeration value in the enumerated variable MyDate, It is output today is wednes26 case (int) MyDate. wed: Console. writeLine ("Today is Wednesday"); break; 27 // if k is equal to the Thi enumeration value in the enumerated variable MyDate, It is output today is thday 28 case (int) MyDate. thi: Console. writeLine ("Today is Thursday"); break; 29 // if k is equal to the Fri enumerated value in the enumerated variable MyDate, the output today is Friday 30 case (int) MyDate. fri: Console. writeLine ("Today is Friday"); break; 31 // if k is equal to the Sat enumerated value in the enumerated variable MyDate, the output is Saturday 32 case (int) MyDate. sat: Console. writeLine ("Today is Saturday"); break; 33} 34 Console. readLine (); 35 36} 37}

Ii. type conversion

Type conversion converts one type to another. Implicit conversion or display Conversion

1. implicit conversion

Implicit conversions can be performed without declaration.

Source Type Category
Sbyte Short, int, long, folat, double, decimal
Byte

Short, ushort, int, uint, long, float, double, or decimal

Short Int, long, ulong, float, double, or decimal
Ushort

Int, uint, long, ulong, float, double, or decimal

Int Long, float, double, or decimal
Uint Int, uint, long, float, double, or decimal
Char Ushort, int, uint, long, ulong, float, double, or decimal
Float Double
Ulong Float, double, or decimal
Long

Float, double, or decimal

 

 

 

 

 

 

 

 

 

 

 

 

 

Implicitly converts an int value to a long value.

1 int I = 927; // declare an integer variable I and initialize it to 927 2 long j = I; // implicitly convert it to the long type

2. Display Conversion

Explicit conversions can also be called forced conversions. the type to be converted needs to be explicitly declared in the code. If you want to convert between non-existing implicit conversion types, you need to use display conversion.

Explicit type conversion table

Source Type

Category

Sbyte

Byte, ushort, uint, ulong, or char

Byte

Sbyte or char

Short

Sbyte, byte, ushort, uint, ulong, or char

Ushort

Sbyte, byte, short, or char

Int

Sbyte, byte, short, ushort, uint, ulong, or char

Uint

Sbyte, byte, short, ushort, int or char

Long

Sbyte, byte, short, ushort, int, uint, ulong, or char

Ulong

Sbyte, byte, short, ushort, int, uint, long, or char

Char

Sbyte, byte, or short

Float

Sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal

Double

Sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal

Decimal

Sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Explicit conversions include all implicit conversions and Explicit conversions, so you can always use a forced conversion expression to convert any numeric type to any other numeric type.

Create a console application and convert the double Type x to the display type

1 static void Main (string [] age) 2 {3 double x = 19810927.0112; // create the double type variable x4 int y = (int) x; // explicitly convert to the variable type Y5 Console. writeLine (y); // output the integer variable y6 Coneole. readLine (); 7}

You can also use the Convert keyword to explicitly Convert data types;

Create a console application and use the Convert keyword to perform explicit type conversion.

1 double x = 19810927.0112; // create the double type variable x2 int y = Convert. toInt32 (x); // use the Convert keyword to Convert to 3 Console. writeLine (y); // output the integer variable y4 Console. readLine ();

3. packing and unpacking

The process of converting a value type to a reference type is called packing. On the contrary, the process of converting a reference type to a value type is called unpacking.

Packing:

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.