C # class and object learning,

Source: Internet
Author: User

C # class and object learning,

A class is a reference type and a custom type.

Style:

* Class name * {* field member * function member *}

When no explicitly declared instance constructor exists in the class, the system will provide it with a public no-argument instance constructor by default.

If another instance constructor exists, the system will not provide it by default. If you want to operate the non-argument instance constructor, You need to explicitly declare it yourself.

The instance fields in the class do not require a value assignment in the constructor.

The instance constructor is used to create and initialize an object.

Class Person {// The instance field in the class can be initialized string name = "lily"; // The system will initialize uninitialized instance fields in the class by default, the initial value is the type default value int age; // No parameter public instance constructor public Person () {}// public Person (string name, int age) {this. name = name; this. age = age;} public void ShowInfo () {Console. writeLine ("name: {0} age: {1}", name, age) ;}} class Program {static void Main (string [] args) {// declare that the referenced variable Person person = null; // when the referenced variable is null, the instance Member cannot be accessed, causing a crash. // person. showInfo (); // create object person = new Person (); person. showInfo (); // change the point of person to point to a new object person = new Person ("Coco", 17); person. showInfo (); // use the referenced variable to assign a value to another referenced variable. The two referenced variables point to the same object Person person00 = person; Console. readKey ();}}

Related Article

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.