The C # Programming Language notes

Source: Internet
Author: User

1. Incomplete Abstract

Even abstract class cannot turn a blind eye to a member of the implemented interface, that is, the following is not allowed:

Interface in_one {
Void ();
}

Abstract class base_one: in_one {
}

This is still convenient for Java, no matter what the original intention of C # Is

2. pointer of the out parameter and pointer

The out subclass cannot be converted to the out base class. If conversion is allowed, the type security cannot be guaranteed. That is, the out parameter is equivalent to the pointer, the pointer of the parent class has no inheritance relationship with the pointer of the subclass, so it cannot be converted.

3. Force interface-specific programming

Interface Programming is just a general principle, but C # provides a mechanism to force the client programmer to reference your implementation Class Based on the Interface: explicit interface member

Interface {
Void ();
}

Class a_sub: {
Void A. (){
}
}
Static void main (string [] ARGs)
{
(A) New a_sub (). A (); // OK!
New a_sub (). A (); // error!
}

The explicit interface member Implementation Program has different access capability characteristics compared with other members. Because explicit interface members cannot access through classes, they are considered private. However, since they can be accessed through interface reference, they are public in a sense.
The explicit interface member implementation program mainly serves two purposes:
A. the class or structure implements an internal interface that the customer is not interested in.
B. Use the same signature to eliminate ambiguity of interface members.
To make the explicit interface member implement the Program effective, the class or member must name an interface in its basic class list, which contains a member, all its valid names, types, and parameter types must be exactly the same as those of the explicit interface member implementation program.

4, @ cancel escape

It is understandable to cancel the string escape, but even the keywords are changed to common identifiers.

5. struct Constructor

The custom ctor cannot hide the default no-argument constructor, which is slightly unexpected but reasonable. The default no-argument constructor has a well-defined meaning for struct.

6, const, readonly

Const is equivalent to static const of C ++
Readonly is equivalent to const of C ++
Therefore, const is equivalent to static readonly. Of course, there are some differences. The const obtains the value during compilation, and the static readonly operation
Public class color {
Public const color black = new color (0, 0, 0); // Error
Public static readonly color white = new color (255,255,255); // OK
Private color (byte R, byte g, byte B ){
}
}

7. Natural boxing and unboxing

String S = "ABC ";
Object o = s;

Int I = 123;
Object o = I;

I am not surprised at the string example. Why do I have to treat int in particular? String is the alias of system. String. As long as int is used as the alias of system. int32, isn't everything natural? Int is indeed the alias of system. int32. int and string are different. The value after boxing is copied, And the array call is clear and will not be extended to the numerical array.

Maybe it's the reason for efficiency.

8. Smart custom Transformation

C ++ is not allowed to call custom transformation operators more than once during a conversion. C # is also not allowed, the difference is that C # inserts a standard transformation operation that is automatically searched for the source type and target type before and after calling the custom transformation operator, if necessary; simply put;

9. Operator Overloading

The memory in C ++ is managed by the programmer and allows the new operator to be reloaded. C # Naturally prohibits the reload of new.
After the Arithmetic Operators +-*/In C ++ are overloaded, the combined form of + =,-=, * =,/= will not be automatically overloaded. C # To ensure semantic consistency, the combination of them is automatically reloaded.

Short-circuit logical operators in C ++ &, | do not have the short-circuit feature after heavy load. C # Simply disables heavy load on them and can only be modified in the following form, the short-circuit semantics is maintained:
Class test {
Public static bool operator false (test ){
Return false;
}
Public static bool operator true (test ){
Return false;
}
Public static test operator & (test LHS, test RHs ){
Return LHS;
}
Public static test operator | (test LHS, test RHs ){
Return LHS;
}
Static void main (string [] ARGs)
{
If (new test () & New Test ()){
}
}
}

10, Scope

Unlike Java, identifiers with the same name in a nested block must reference the same entity. This rule ensures that the meaning of a name in an expression context is the same in a block:
Class Test
{
Double X;
Void F (bool B ){
X = 1.0;
If (B ){
Int x = 1;
}
}
}
Yes, because X references different entities in the external block (including the extension of nested blocks in the IF Statement. On the contrary, examples
Class Test
{
Double X;
Void F (bool B ){
If (B ){
X = 1.0;
} Else {
Int x = 1;
}
}
}
Yes, because name X is never used in the external block.

11. Leave finally

Like java, an exception is thrown in finally, and the original exception (if any) will be terminated. The difference is that return and goto cannot be used to leave finally. I wonder why.

12. Sub-packages and peripheral packages

Unlike Java, the sub-package can access the peripheral package without using, which is convenient, but is it reasonable?

13. Attribute Permissions

GET/set seems to be unable to set different permissions, for example, I want to internal set, but public get, I don't know how to get it

14. serialization

When the vast majority of fields can be serialized by default, and a few require special processing, they are made into an internal independent object to customize serialization, and then the original object contains this independent object.

15, int [] [] is a one-dimensional array

Int [,] is two-dimensional, Matrix
Int [] [] is actually a sawtooth

Int [] [] One = new int [7] [];
Int [,] Two = new int [3, 2];

The dimension indicator is read from left to right before the last non-array element. For example, int [] [,] [,] is a single-Dimension Array of a three-dimensional array of two-dimensional arrays of the int type.

The array call explicitly does not extend to an array of the numeric type. For example, int [] can be considered as an object [] conversion if it does not exist.

The array initialization program must have the same nested level as the array dimension. The outermost layer nesting corresponds to the leftmost dimension, while the innermost layer nesting corresponds to the rightmost dimension.

16. Does new virtual mean anything?

Class
{
Public Virtual void F () {console. writeline ("a.f ");}
}
Class B:
{
Public override void F () {console. writeline ("B. F ");}
}
Class C: B
{
New public virtual void F () {console. writeline ("C. F ");}
}
Class D: c
{
Public override void F () {console. writeline ("D. F ");}
}
Class Test
{
Static void main (){
D = new D ();
A A = D;
B = D;
C = D;
A. F ();
B. F ();
C. f ();
D. F ();
}
}
Classes C and D contain two virtual methods with the same signature: one is introduced by a and the other is introduced by C. The method introduced by C hides the method inherited from. In this way, the overwrite statement in D overwrites the method introduced by C, and it is impossible for D to overwrite the method introduced by. The example generates the following output:
B. f
B. f
D. f
D. f

Under what circumstances and for what reason do we need new virtual?

17. Access modification cannot be changed during override.

Even if it is relaxed

18, x + = Y. The result may be void.

"In an operation in the form of X + = Y or X-= Y, when X is an event member and references occur outside the type of X, the operation result is void. This rule prohibits external code from directly checking event members "???

19. constructor execution sequence

An instance variable initialization function and a constructor initialization function can be regarded as automatically inserted before the first statement in the constructor. Example
Class
{
Int x = 1, y =-1, count;
Public (){
Count = 0;
}
Public A (int n ){
Count = N;
}
}
Class B:
{
Double sqrt2 = math. SQRT (2.0 );
Arraylist items = new arraylist (100 );
Int Max;
Public B (): This (100 ){
Items. Add ("default ");
}
Public B (INT N): Base (n-1 ){
Max = N;
}
}
It contains many variable initialization functions and constructor initialization functions of every form (base and this. This example is related to the example below.

The annotation specifies an automatic insert statement (the syntax used to call the automatic insert constructor is not valid, at least to demonstrate this mechanism ).
Class
{
Int X, Y, count;
Public (){
X = 1; // variable initializer
Y =-1; // variable initializer
Object (); // invoke object () constructor
Count = 0;
}
Public A (int n ){
X = 1; // variable initializer
Y =-1; // variable initializer
Object (); // invoke object () constructor
Count = N;
}
}
Class B:
{
Double sqrt2;
Arraylist items;
Int Max;
Public B (): This (100 ){
B (100); // invoke B (INT) constructor
Items. Add ("default ");
}
Public B (INT N): Base (n-1 ){
Sqrt2 = math. SQRT (2.0); // variable initializer
Items = new arraylist (100); // variable initializer
A (n-1); // invoke a (INT) constructor
Max = N;
}
}
Note that the variable initialization function is converted to a value assignment statement, and that value assignment statement is executed before calling the base class constructor. This Order ensures that all instance domains are in any instance access statement.

Before execution, they are initialized by their variable initialization function. For example:
Class
{
Public (){
Printfields ();
}
Public Virtual void printfields (){}
}
Class B:
{
Int x = 1;
Int y;
Public B (){
Y =-1;
}
Public override void printfields (){
Console. writeline ("x = {0}, y = {1}", x, y );
}
}
When new B () is used to create B's instance, the following output is generated:
X = 1, y = 0
Because the variable initialization function is executed before the base class constructor is called, the value of X is 1. However, the value of Y is 0 (the default value of INT), because the value of Y is not executed until the return of the base class constructor.

Static constructors are automatically called and cannot be explicitly called. Although many constraints are provided, the exact time and order of static constructor execution are uncertain:
The static constructor of a class is executed before any instance of the class is created.
The static constructor of a class is executed before any static member of the class is referenced.
The static constructor of a class is executed after the static constructor of all its derived classes is executed.
The static constructor of a class is never executed more than once.

20. struct can implement interfaces

Unexpectedly, it seems that struct is basically similar to the "Sealed class with deep copy semantics for basic data type fields". After that, I felt awkward.

21. Interface

Unlike Java, the interface cannot declare static constants and internal classes. The interface must be composed of methods, attributes, events, or indexes.

22, enumeration

I don't understand why it has to be related to an integer. For the sake of & | operation? Even in this way, you can use an integer internally without exposing it.

23, delegate

The observer mode supports language, which is better than Java. util. observable. delegate is type-safe. Unlike java. util. observable, delegate is not exceptionally secure.

24. condition attributes

By marking the called party, the code can be compiled in place of the traditional caller's condition, so that the code is neat and the single-point switch can eliminate the disadvantages of macros.

(The Java programming language 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.