C # basic syntax Learning (2 ),

Source: Internet
Author: User

C # basic syntax Learning (2 ),

1. Object-oriented thinking has several important features: Abstraction action, Encapsulation, Inheritance, and Polymorphism ).
Abstraction is to filter the nature and behavior of things in complex real life, select information that makes sense for the software to be developed, and remove useless parts, finally, a software object describing only some useful information of a real thing is established.
Encapsulation is to hide internal data and implementation details to users. Encapsulation plays two important roles. First, through encapsulation, the interface information exposed by the object is simpler, making it easier for users to use the object. Second, through encapsulation, the object user does not know the specific implementation details of the object, thus eliminating the object

Tight coupling between the user and the object implementation details.
Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class. The new class created by inheritance is called a subclass or a derived class ". The inherited class is called "base class", "parent class", or "super class ". The process of inheritance is from general to special.

C # can only implement Single inheritance.
Polymorphism is a technique that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class.

2. The class definition in C # is as follows:
[Access modifier] class name
{
[Variable definition]
[Method definition]
[Attribute definition]
....
}
The class can define variables, methods, and attributes. The class has an access modifier used to describe the visibility of the class. You can also define other classes in a class. The classes defined in a class are called Nested classes or internal classes. In most cases, the class modifier can only be public or internal,

Only the access modifier of the nested class can be protected, protected internal, or private.
Classes also have static and non-static classes. A static keyword exists before a static class is declared. A static class can only contain static members and cannot create instances of static classes. In addition, static and non-static classes are the same.

3. constructor is a special method of the class. Constructor is similar in form and function to a method without return values. It is always executed when an object is created. There is no way to call it elsewhere.
Note the following two points for Constructor: 1. the constructor name must be the same as the class name, including case sensitivity. 2. The constructor does not return values. void is not allowed to indicate that no value is returned.
When the new keyword is used to create an instance of a class, the constructor is called. The main function of the constructor is to initialize the class. Each non-static class must have at least one constructor. Only instances of the class can be created.
If no constructor is declared for the class in C #, C # automatically generates a default constructor with the method body empty, if any constructor is declared, C # will not automatically generate the default constructor.

4. Object Instantiation and usage. The syntax is as follows:
Variable name = new Class Name (constructor parameter );

5. C # garbage collection mechanism: C # uses the Common Language Runtime (CLR) for garbage collection. When the available memory reaches a certain threshold, CLR will automatically call the garbage collection function to sort out the memory and recycle useless memory space.
When performing garbage collection, you must first determine which memory is still in use and which memory is useless. When a class variable is declared and an instance of the class is created, CLR allocates a memory area for the class. When the variable is out of scope, the corresponding memory area becomes a useless memory area,

This memory area should be recycled for reuse during garbage collection. CLR determines whether the memory is still in use by judging whether the memory can be referenced by a variable. CLR divides all memory references into two types: Root reference and non-root reference. Root Reference refers to the local variables of the current activity and static variables of the class,

A non-root reference is usually an instance field of the class. If a memory area can be accessed directly or indirectly from a reference, the memory area will be put into use. Otherwise, it indicates that the variables that reference this memory area are out of scope, this memory cannot be accessed and should be recycled.

6. The Destructor corresponds to the constructor and is the function executed when the object is destroyed. C # The class destructor name is in "~" The name of the class following the symbol. If the class name is Car, The Destructor name is ~ Car.
The destructor in C # is executed before the object is recycled by the garbage collector. When the CLR wants to recycle an object, it first checks whether the object has any destructor. If so, the garbage collection will not release the object's memory, instead, the object is put in the Destructor queue.

CLR creates a special thread to call the destructor of objects in the analysis queue. After an object's destructor is called, the memory occupied by this object will be recycled in the next garbage collection.
Since the CLR only recycles garbage when the memory reaches a certain level, that is, the garbage collection time is uncertain, so the execution time of the C # object destructor is also uncertain. Do not place resources (such as database connections and file handles) in the destructor,

Otherwise, these resources cannot be released in time. The correct way to release resources is to write a method for clearing resources that can be displayed and called in the class, and then call the method for clearing resources after using the object .. NET Framewor defines an IDisposable interface,

It contains a Dispose method to implement IDisposable and Dispose. It is a common method to implement resource recycling in C.

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.