C # syntax details

Source: Internet
Author: User

I carefully read the C # programming guide in msdn these days, and noted down some details by the way:

C #:
Structures can implement interfaces, but they cannot inherit from another structure. Therefore, the structure member cannot be declared as protected.
Explicit type conversion is required for the conversion from the enum type to the integer type.
It is incorrect to use both new and override for the same member because the meanings of the two modifiers are mutually exclusive. The new modifier creates a new member with the same name and hides the original member. The override modifier extends the implementation of inherited members. The class is the reference type, while the structure is the value type. The reference type is allocated in the heap. Memory Management is handled by the garbage collector. Value types are allocated on the stack or in combination and released when the range is exceeded. Generally, the cost of Value Type allocation and release is smaller. However, if a large number of packing and unboxing operations are required, the value type is not as good as the reference type.

Abstract classes should not be instantiated under any circumstances. Therefore, it is very important to define their constructors correctly. It is also important to ensure the correctness and scalability of abstract classes.
You cannot use C # to override the internal virtual method.

The protected members of the base class are accessible only when they are accessed by the derived class type. They can only be accessed through the "derived class!
Private Members are accessible only when their classes and struct are declared.

Static classes and class members are used to create data and functions that can be accessed without the need to create classes.

The main functions of static classes are as follows:

They only contain static members.

They cannot be instantiated.

They are sealed.

They cannot contain instance constructors.

 

Static member: You can call static methods, fields, attributes, or events in the class even if no instance of the class is created. If any instance of this class is created, the instance cannot be used to access static members.
Static members are usually used to represent data or calculations that do not change with the object state. For example, a mathematical library may contain static methods used to calculate the sine and cosine.
Static methods can be overloaded, but cannot be overwritten.
The Destructor cannot be defined in the structure. Only destructor can be used for classes.

A class can have only one destructor.
You cannot call the destructor. They are automatically called.

The Destructor neither have modifiers nor parameters.

The readonly keyword is different from the const keyword. The const field can only be initialized in the declaration of this field. The readonly field can be initialized in the Declaration or constructor. Therefore, the readonly field may have different values based on the constructors used. In addition, the const field is the compile time, while the readonly field can be used to run the time.

String objects cannot be changed after they are created.
String S1 = "hello ";
String S2 = S1;
S1 + = "And goodbye .";
Console. writeline (S2); // outputs "hello"

Escape double quotation marks with another double quotation mark character in the original literal string, as shown in the following example:
String S = @ "you say" "goodbye" "and I say" "Hello """;

If the instance can be accessed by the public, the lock (this) problem occurs.

If mytype can be accessed by the public, the lock (typeof (mytype) problem will occur.
A good practice is to define private objects to lock or private static object variables to protect the data shared by all instances.

Unless the class is static, the C # compiler provides a common default constructor for classes without constructor so that the class can be instantiated.

The constructor of the structure type is similar to the constructor of the class, but structs cannot contain the explicit default constructor, because the compiler will automatically provide a constructor. This constructor initializes each field in struct to the default value. Both class and structs can define constructors with parameters. Constructors with parameters must be called using the new or base statements. Class and structs can also define multiple constructors, and neither of them needs to define the default constructor.
The base class constructor is called before the constructor block is executed.
It is incorrect to use the base keyword in a static method.
The Destructor cannot be defined in the structure. Only destructor can be used for classes.
A class can have only one destructor.

Application domains enable data separation between applications and applications, helping improve security. A single process can run multiple application domains and has an isolation level in a single process. Running multiple applications in a single process improves server scalability.
The Assembly must be loaded into the application domain before it can be executed. For more information, see program set and Global Assembly Cache (C # programming guide ).

Errors in one application domain do not affect other code running in another application domain.

You can stop a single application and uninstall the code without stopping the entire process. You cannot detach an independent assembly or type. You can only detach the entire application domain.

 

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.