C # Basic Class notes

Source: Internet
Author: User

1, static and non-static difference: whether there is static

Non-static:

1) in a non-static class, you can have either an instance member or a static member

2) in the invocation instance member, through the object. Instance members

When a static member is called, the class name is passed. Static Members

Static:

1) Static classes can only have static members, cannot have instance members

2) A static class cannot be instantiated, that is, the object cannot be created

Summary: Static members must be called using the class name, and instance members use objects to invoke

Only static members can be accessed in a static function, and access to instance members is not allowed

Instance methods can access both instance members and static members

Use of static classes:

1) If you want your class to be a "tool class", consider writing a static

2) Static classes share resources throughout the project

Freeing Resources (Gc,garbage Collection garbage Collector): Static classes can release resources only after the program has finished all

2. Constructor function

Definition: The construction method is used to create the object, and the object can be initialized in the constructor. Reduced redundancy of the code

Role: Helps us initialize the object (assigning values to each property of the object in turn)

A constructor is a special method:

1) No return value, no write void

2) The constructor name must be the same as the class name

Public Student () {}

The access modifier for the constructor must be public

Constructor execution: The constructor executes when the object is created

Constructors can be overloaded (with the same function name, with different parameters)

There is a default parameterless constructor in the class, and when you write a new constructor, whether it is a parameter or a parameter, the default is eliminated.

3. New keyword

New has done three things for us:

1) opens up a space in memory

2) Create objects in the open space

3) Call the object's constructor to initialize the object-so the constructor's access modifier must be public

4. This keyword

Effect: 1) Represents the object of the current class this. Name=name;

2) The constructor for this class is explicitly called in the class public Student (string name,int age,char gender): This (name,0,gender)//Call the name and gender of this class constructor

5. destructor

Syntax: ~student () {function Body}

Function: When the program ends, the destructor executes, releasing the resource

C # Basic Class notes

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.