C # Learning 10

Source: Internet
Author: User

C # Learning 10: enumeration and struct

An Enumeration type is a set of named constants. Each Enumeration type has a basic type. All Integer types except char can be used as the basic type of an enumeration type.

The structure type is a composite data type that contains constants, fields, methods, attributes, indexers, nested types of operators, struct

Suitable for lightweight objects such as points, rectangles, and colors

Defines an enumeration type and two struct types to implement some basic operations.

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace com {Enum season {spring, summer, fall, winter}; struct time {public int hours, minutes, seconds; Public time (int h, int M, int S) {hours = H; minutes = m; seconds = s;} public time (int h, int m) {hours = H; minutes = m; seconds = 0; // each field must be assigned a value, which is different from the class} public int hours () {return hours;} public int minutes () {return minutes;} public int seconds () {return seconds ;}} struct Date {public int year; Public season s; public date (int A, season B) {This. year = A; // This is the keyword, indicating the current instance of the class this. S = B ;}} class program {static void main (string [] ARGs) {season S = season. spring; console. writeline (s); season I = 0; Int J; For (j = 0; j <5; j ++) // The enumerated type starts from 0 {console. writeline (I); I ++;} time t1 = new time (); console. writeline (t1.hours (); // The default value of the struct is 0 time t2 = new time (10, 20, 30); console. writeline (t2.hours (); time t3 = new time (10, 20); console. writeline (t3.hours (); Date = new date (hz0); console. writeline ("Year: {0}, season: {1}", date. year, date. s );}}}

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.