C # using constants \ enumerations \ structures and Arrays,

Source: Internet
Author: User

C # using constants \ enumerations \ structures and Arrays,

The next day is over. I will continue to summarize the knowledge I learned from cloud and the college.

I. Use of constants \ enumerations \ Structures

Theory:

Constant:Const type constant name = constant value

Determine the quantity and values: East-West, North-South, men and women, and upper-and Middle-down.

Enumeration:Enum Gender {male, female}

String and enumeration Conversion:( Gender) (Enum. Parse (typeof (Gender), "male"); each value corresponds to a number

Struct:Access modifier struct structure name

{

Define structure members

}

Practice:

1, const

Tatic void Main (string [] args)

{

Const int PI = 3.14;

PI = 3.20;

}

2, enum

Enum QQStatus {online, offline, hidden}

QQStatus status = QQStatus. online;

3. Use of struct and enumeration

• Define a structure type Person with three members: name, gender, and age and gender. Use Enumeration type-declare two Person-type variables, they indicate that Zhang San male is 18 years old/Xiao Lan female is 16 years old
Class Program {public enum Gender {femal, meal}; public struct person {public string name; public int age; public Gender gender;} static void Main (string [] args) {// defines a structure type Person, which has three members: Name and gender. for age and gender, use Enumeration type. // declares two Person-type variables, it indicates Zhang San male 18 years old/Xiao Lan female 16 years old person p1 = new person (); p1.name = "Zhang San"; p1.gender = Gender. femal; p1.age = 18; person p2 = new person (); p2.name = "Alan"; p2.gender = Gender. meal; p2.age = 16; Console. writeLine ("Hello everyone, I'm {0}, I'm a {1} student, {2} years old this year", p1.name, p1.gender, p1.age); Console. writeLine ("Hello everyone, I'm {0}, I'm a {1} student, {2} years old this year", p2.name, p2.gender, p2.age); Console. readKey ();}}

2. Array
Theory:

Saves multiple values. Arrays can be declared for almost any type; int [] nums = new int [3]; int [] nums = {5, 3, 8 };

In [] nums = new int [3] {1, 2, 3}; int [] nums = new int [] {2, 3, 5 };

Int [] nums = new int [3] {5, 3, 8} // The number must be consistent with the declared number.

Int [] nums = new int [5] {5, 3, 8} // Error

Int [] nums = new int [] {5, 3, 8} // correct. array characters can be omitted.

Array declaration: the above four types

Use the indexer to access the elements at the specified number position and access the array elements: nums [0] And nums [1]. The index starts from 0. The element type obtained is the type of the array element. You can also assign values to array elements.

FAQs: Average, maximum, sum, and sorting

Array sorting and Reverse Order

Practice:

The array contains the name of a person, which is divided into: for example, Lao Yang | Lao su | Lao Miao ..."

String names = ""; string [] name = new string [8] {"Lao Yang", "", "Lao Jiang", "Lao Wang", "Lao Ma"}; for (int I = 0; I <name. length-1; I ++) {names + = name [I] + "|" ;}console. write (names + name [name. length-1]); Console. readKey ();

Process each element of an integer array as follows: if the element is a positive number, add the value of the element at this position to 1, if the element is negative, the value of the element at this position is reduced by 1. If the element is 0, the value remains unchanged.

For (int I = 0; I <num. length; I ++) {Console. writeLine (num [I]);} Console. writeLine ("----------------------------"); for (int I = 0; I <num. length; I ++) {if (num [I] <0) {num [I] --;} else if (num [I]> 0) {num [I] ++;} else {}
Console. WriteLine ("Current Value: {0}", num [I]);}
Console. ReadKey ();

Reverses the order of elements in a string array. {"I", "yes", "Good Guys"} {"Good Guys", "yes", "I "}. Exchange Between I and length-i-1

String [] str = {"I", "yes", "good guy"}; for (int I = 0; I <str. length/2; I ++) {string temp = str [I]; str [I] = str [str. length-1-I]; str [str. length-1-I] = temp;} for (int I = 0; I <str. length; I ++) {Console. write (str [I]);} Console. readKey ();

Obtains the maximum integer, minimum integer, sum, and average value from an integer array.

Int [] num = {51, 21, 82, 41, 10,-5}; int max = 0; int min = 0; int sum = 0; for (int I = 0; I <num. length; I ++) {if (num [I]> max) {max = num [I];} else if (num [I] <min) {min = num [I];} sum = sum + num [I];} Console. writeLine ("maximum integer: {0}, minimum integer: {1}", max, min); Console. writeLine ("sum is {0}, average is: {1}", sum, sum/num. length); Console. readKey ();

Now, let's write it here today. Continue tomorrow! Come on!

 

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.