Three kinds of complex variable types in C #

Source: Internet
Author: User
Tags arrays

The variables described earlier have a basic range of values, except for the string type, where each type of variable has only a certain number of bytes in memory, and the following 3 user-defined complex variables are described below.

A enumerated type

Define enum type: Keyword enum

How to use:

Enum ENAME

{

Value1,value2,............. Valuen

}

Declares an enumerated variable ename myenum=ename.value of a ename type, by default the value of the type of the enumerated type is int,value1,value2, which is incremented by default from 0 to 1. You can also use other types. Please look at the example:

Enum days

{

Monday=1,

tursday=2,

Wensday=3,

Thursday=4,

Friday=5,

Saturday=6,

Sunday=7

}

Enum Days:short

{

}

Use

Days myday = Days.friday;

Console.WriteLine ("Myday is{0}:", Myday);

The output is shown in the following illustration:

Define the location of the enum

The basic types of enumerations can be Byte,sbyte,long, Short,ushort,int, Uint,ulong.

Two Structural body

The structure is defined and used in the same way as the enumeration type, only the variables defined in the structure can have their own values, whereas in an enumerated variable the value can only be one of the enumerated values, and in the example above the days Myday can only be one day of one weeks. Again, let's look at the process of using a struct.

struct Cloth//structural body name

{

public int age;//Clothing service life

public string sex;//appropriate sex

public string style;//Style

Public double price;//Price

Public double count;//Discount

}

Cloth Mycloth;

Mycloth.age = 2;

Mycloth.count = 0.98;

Mycloth.price = 999.99;

Mycloth.sex = "Man";

Mycloth.style = "trousers";

The output is:

Console.WriteLine ("Structure Data output:");

Console.WriteLine ("Age={0},sex={1},style={2},price={3},count={4}", Mycloth.age,mycloth.sex,mycloth.style, Mycloth.price,mycloth.count);

The result:

Three Array

Arrays are used to store a large number of types as well as data of different values. For example, in a class of student information, use an array of strings to save all the students ' names.

string[] Stunames = new STRING[5];

Stunames[0] = "country";

STUNAMES[1] = "people";

STUNAMES[2] = "China";

Stunames[3] = "Han nationality";

STUNAMES[4] = "Chairman";

foreach (String sname in Stunames)

Console.WriteLine (sname);

Output Results Chart:

The use of arrays includes array declaration and initialization, and when defining an array, the length of the array is a constant and cannot be represented by a variable: for example: int[] score = new INT[7]; int []score=new int [const Size]; or int[] Englishscore ={1, 5, 6, 8, 5, 7, 74};

An array must be initialized before it is accessed, and an error occurs if it is defined but not initialized.

Multidimensional Arrays: You can define the number of different dimensions according to need, one dimension, two-dimensional array use more, and more than 3-D array use frequency is rare. For example, to define a two-dimensional array to represent the results of three courses for 30 people in a class, double [] stuscore=new double[30][3], then stuscore[1][2] represents the result of the third course of the second student, which is not much to say, Everyone should be programmed to experience it personally.

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.