Understanding C # Arrays, understanding array types, learning definitions, and initialization

Source: Internet
Author: User

Here's the outline of the PPT I used to explain the array to the students (bold), because of my lack of knowledge, may be in a lot of knowledge to grasp, so I will give students to explain the statement (not bold) at the same time write, I hope to have passed the teaching plan of the people to give advice, How to explain to students who may have a basic knowledge of C, and let them understand the usage of their actual projects.

One: Let's get to know arrays, understand array types, learn definitions, and initialize

An array is a set of values of the same data type

First we'll introduce a few concepts to help you understand arrays. An array is a set of variables that have the same type and name, such as a group of integers, a set of characters, and so on. These variables that make up the array are called the elements of the array, the number of elements is also called the length of the array, the capacity of the array, once the capacity is created, it is fixed, the number of elements of this array can not be increased or decreased.

Each array element has a number, which is called the subscript, which is also called an index, an array of n elements, and its index is from 0~n-1. The attributes of these array objects, indexed and capacity, are fixed when an array object is created, and when indexed operations are used, the defined capacity cannot be exceeded to avoid an error in which the array index exceeds the bounds.

In the C language, the array will also have the index out of bounds, but in general will not be reflected in the compilation, may be smooth through, but for your program has brought hidden dangers, if the user does not operate properly, exceeding capacity, the program will appear errors. VS to solve this problem, at compile time to find out the error will be out of bounds, the risk of staying in the programming phase, reducing the instability of the program.

Students are now a little puzzled, why to use an array to store data? Do I have to define multiple variables to store the data? What's the advantage of using an array?

Let's use the next feature of the array to explain it!

The array is a reference type, stored in the managed heap memory, and the pointer is on the thread stack.

The value of a reference type that allocates a specific location in the managed heap to hold the data. The memory address that points to the data store is then stored in the thread stack because the array is a reference type, so that it adds and modifies elements in the array and arranges the elements together according to the index, like the student number in the class. You can operate on students ' grades and information according to the school Number (index), you can simply define an array variable, and remember its name to make it easier to do all the things you want to achieve. Not like a simple numeric type, you have to remember the name of each student in the class to work, modification or deletion of information is inconvenient, and their storage location is not necessarily continuous, which will reduce the efficiency of the program, so you can use the array, organize the data to be managed, for example, record by day, month ... Consumption of chronological, record the efficiency of employees and so on.

Next, let's look at the definition and the initial recognition of the array.

Definition and initial recognition of one-dimensional arrays:

Type[] arrayname={value1,value2,......, Valuen}; Defines a data type that is assigned at the same time.

Type[] arrayname=new type[n]{v1,v2,......, VN}; Use the New keyword definition to assign values at the same time. Here the n can be omitted, because the defined type has already assigned n initial values.

For the first time here, the new keyword appears, it is commonly used in C # and you are now more difficult to understand the concept, you need to know that the process of new is actually to the array in the managed heap allocated a position, to store the data can be, in the future we learn the class will be specifically explained. The next thing I want to see is a definition that I want to emphasize.

Array element initialization or assignment to an array element can be made either at the time the array is declared or at the later stage of the program. That is, you can define it first, as in the following example, but this is to write N and tell the compiler to define n elements.

Type[] Arrayname=new Type[n]; Define n Elements First

After this definition. NET assigns the same default value to each element in the array based on the type. such as int, assigns each element a value of 0;string type "". When you want to manipulate that element, assign it to the index. Such as:

arrayname[0]=value1; Arrayname[n-1]=valuen;

Type[] Arrayname; This approach is to split the second definition method into two steps

Arrayname=new type[n]{v1,v2,......, VN}; Here the n can also be omitted, the same reason.

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.