C # create an object Array

Source: Internet
Author: User

I have never created an object array in C # before. I wrote a small exercise today and it will not be used when I use an object array.

 

Creating an object array in C # is not like C ++.

 

Code:

 

View plaincopy to clipboardprint?
  1. Student [] student = new student [5];

Student [] student = new student [5];

 

Parameters are not specified with parentheses when an object array is created, unlike creating a single object.

Code for creating a single object:

View plaincopy to clipboardprint?
  1. Student = new student ();

Student = new student ();

At the same time, if the brackets in the preceding array dimensions cannot specify the dimension when creating an object array, if the dimension is specified, an error occurs during compilation:

Student [5] student = new student [5]; // It cannot be compiled here.

 

Note that after creating an object array, no object is generated, but an array referenced by the object is simple. If an object is used at this time, a null pointer reference will appear. When using an array for each reference, you also need to use new to create an object.

View plaincopy to clipboardprint?
  1. Student [] student = new student [5];
  2. Student [0] = new student ();
  3. Student [0]. Age = 12;
  4. Student [1] = new student ();
  5. Student [1]. Age = 13;
  6. Student [2] = new student ();
  7. Student [2]. Age = 74;
  8. Student [3] = new student ();
  9. Student [3]. Age = 34;
  10. Student [4] = new student ();
  11. Student [4]. Age = 32;

I have never created an object array in C # before. I wrote a small exercise today and it will not be used when I use an object array.

 

Creating an object array in C # is not like C ++.

 

Code:

 

View plaincopy to clipboardprint?
  1. Student [] student = new student [5];

Student [] student = new student [5];

 

Parameters are not specified with parentheses when an object array is created, unlike creating a single object.

Code for creating a single object:

View plaincopy to clipboardprint?
  1. Student = new student ();

Student = new student ();

At the same time, if the brackets in the preceding array dimensions cannot specify the dimension when creating an object array, if the dimension is specified, an error occurs during compilation:

Student [5] student = new student [5]; // It cannot be compiled here.

 

Note that after creating an object array, no object is generated, but an array referenced by the object is simple. If an object is used at this time, a null pointer reference will appear. When using an array for each reference, you also need to use new to create an object.

View plaincopy to clipboardprint?
  1. Student [] student = new student [5];
  2. Student [0] = new student ();
  3. Student [0]. Age = 12;
  4. Student [1] = new student ();
  5. Student [1]. Age = 13;
  6. Student [2] = new student ();
  7. Student [2]. Age = 74;
  8. Student [3] = new student ();
  9. Student [3]. Age = 34;
  10. Student [4] = new student ();
  11. Student [4]. Age = 32;

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.