Class and Object

Source: Internet
Author: User
Tags finally block
Class is the implementation of the data type, and the object is the instance of the class.

A class member is an object declared in the class. Class C # has six types of members: field, attribute, indexer, method, event, and nested type.

The C # field is decorated with const, indicating that it can only be initialized when it is declared; The readonly field indicates that it can only be initialized during Declaration or in the constructor. In both cases, the field value cannot be changed after initialization.
Before the object starts to use, all value fields must be initialized, that is, they must be initialized before the end of Object Construction. There are two methods for initialization:

    1. Initialize the class directly when it is declared;
    2. Initialize in the class constructor.

The const field must be initialized during Declaration; otherwise, the compiler reports an error. And the const is implicitly static.
The readonly field should be initialized before the constructor ends. Otherwise, the field will always be null, but the compiler will not report an error.

When the method is declared and called, The ref keyword is used at the same time. C # can force the method to pass Parameters by reference. It can be used in both the value type and the reference type.
When a parameter is marked with the out keyword in the method signature and method call, the parameter can be used as the return value of the method, that is, its value can be returned. In addition, real parameters do not need to be initialized. These parameters must be initialized in the method; otherwise, they cannot be used.
C # Allow methods with parameters of an indefinite number. The syntax for this feature is to use the Params keyword. The parameter that applies the Params keyword must be the last parameter in the parameter table.
Parameters Using the Params keyword can be applied to the object type. This technique can be used to accept various types of parameters.

Method overload: multiple methods of a class use the same name.
The parameter table of the method must be different. The compiler does not consider the type of returned values when distinguishing different methods.

C # Allow objects to provide events. The concepts of events include:
Event subscriber. When an event is triggered, each subscriber will receive a notification. The subscriber can dynamicallyProgramWhen running) Select subscription or unsubscribe events. In C #, the subscriber is represented as a method.
Events can be triggered by objects at any time. Internally, an event knows its subscriber. The responsibility of an event is to notify every subscriber with the event parameters when it is triggered. In C #, except for declarations using the event keyword, the event is similar to a delegate type field in the class.
One of the advantages of using the event keyword (defining events) is to prevent events from being triggered outside the class. Even a subclass derived from a class that defines an event cannot trigger an event, even if the event is virtual.
When an event is triggered, the method for subscribing to the event is executed in the thread that triggers the event.
The subscriber's execution time may be long or cause exceptions. This indicates that the method for initiating an event should not be coupled with the method for subscribing to the event. The subscription method can be executed asynchronously.

The internal visibility level and the internal or protection visibility level cannot be applied to struct members.
Because a private member of Class A can only access it internally, this means that instance I1 of Class A can access the private member of another instance I2 of the same class. A little confused. Please refer to the following link.

If at least one constructor is defined in the class, the compiler will not provide the default constructor.
In the structure, the compiler always provides a default constructor because the constructor without parameters cannot be defined.

The C # language provides a syntax that uses the using keyword to automatically call the dispose () method in a try/Finally block. That is:

Article =   New Article ( 300 );
Try
{
}
Finally
{
A. Dispose ();
}

Equivalent

Article =   New Article ( 300 );
Using ()
{
}

For using (a) using (B) {}, the order of calling the dispose () method at the end of the valid region is to call B. Dispose () first, and then a. Dispose ().
When a class implements the idisposable interface, we recommend that you call dispose () to prevent other methods of the object from being called by triggering the system. objectdisposedexception exception. In order to leave users with more room, we recommend that you allow multiple calls to the dispose () of an object (). Of course, only the first call takes effect.

The Terminator is defined by overwriting the finalize () method of the object class. The structure does not have a Terminator. CLR considers that there is no Terminator for classes that do not override this method.
The Terminator is called when an object is collected by the garbage collector. The memory occupied by the object state will be released during the next garbage collection.

Static methods cannot parse non-static methods, fields, attributes, and events of the class.
A static constructor is also called a class constructor. The declared method is to add the static keyword before it and the parameter cannot be included. It cannot contain a visibility modifier.
C #2.0 run a static class. A static class can only contain static members. C # The Compiler does not provide Default constructors for static classes. Static classes cannot be instantiated because they do not have instance constructors. It cannot inherit a static class. The structure cannot be static. Ee6d64b9

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.