The C # Programming Language Notes

zhaozj2021-02-16  59

1, uncomfortable Abstract

Even the Abstract Class, it is impossible to turn a member of the implemented interface, that is, the following is not allowed:

Interface in_one {void a ();

Abstract class base_one: in_ONE {}

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

2, OUT parameters and pointers of the pointer

The OUT subclass cannot be converted to an OUT base class. If the conversion is allowed, the type security will not be guaranteed; that is, the OUT parameter is equivalent to the pointer of the pointer, and the parent class's pointer is not inherited, so it cannot be Transform

3. Force for interface programming

For interface programming is just a general principle, but C # provides a mechanism, forcing customer programmers to reference your implementation class based on the interface: explicit interface member

Interface a {void a ();

Class A_SUB: A {Void AA () {}} static void main (String [] args) {((a) new a_sub ()). A (); // ok! new a_sub (). A (); / / Error!

Explicit Interface Members Realize the program with different access capabilities compared to other members. Because explicit interface members cannot access the class, they are all private. However, since they can be accessed through the interface, they are also common. Explicit Interface Member Implementation Program Main Services: A, class or structure Implement an internal interface B that is not interested in, using the same signature to eliminate interface members to make the explicit interface member implementation effective, class or members You must name an interface in its basic class list, which contains a member, all of its valid name, type, and parameter types and explicit interface members implement priority

4, @ cancel escape

Canceling string rings can still be understood, but even the keywords become ordinary identifies, do not seem to have much meaning

5, Struct constructor

Custom CTORs do not hide the default non-refined constructor, slightly unexpected, but also in the reason, the default non-refined constructor has a good definition of Struct

6, Const, Readonly

Const equivalent to C Static ConstreadOnly is equivalent to C Const, constative is equivalent to static readonly, of course, still some differences, CONST compiles value, Static Readonly runtime 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, unboxing

String s = "abc"; Object O = S;

INT i = 123; Object O = i;

Not surprised for String examples, why should I treat Int's examples? String is the alias of System.String, just treat Int as the alias of System.Int32, is it not very natural? INT is indeed the alias of System.Int32, INT and STRING still have different differences. The value of boxing will be copied, and the array calls clearly not extended to numerical types may be efficient.

8, intelligent custom transformation

C does not allow one-time transition to call more than once custom transformation operators, and C # is also not allowed, and the difference is that C # will be inserted with the source type and target type before and after calling the custom transformation operator. If needed; simply;

9, operator overload

C memory is managed by programmers, allowing the new operator, and C # is forgiven for prohibiting the reload of NEW's heavy-duty C arithmetic operator - * / and so on, its combination form =, - =, * =, / = Will not be overloaded automatically, C # In order to ensure semantic consistency, the combination of combination is automatically overloaded.

C short-circuit logic operators &&, || The short-circuit feature is no longer, C # simply prohibits the overloading of them, can only be varied in the following form, actually maintained a Semantic Semantic: Class Test {public static bool Operator false (test test) {Return False;} public static bo {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, the role

It is a bit different from Java, and the identifier in the nested block must reference the same entity. This rule guarantees the meaning of the name in an expression context to be the same in one block: Class test {Double X; Void F (BOOL B) {x = 1.0; if (b) {int x = 1;}}} is incorrect because X is referenced in the external block (extension in the IF statement contains the nesting block) references different entities. Instead, examples of class test {Double X; Void F (BOOL B) {if (b) {x = 1.0;}}}}}}} is allowed, because the name X will never be in the external block use

11, leave Finally

Like Java, Finally throws an abnormality, the original exception (if any) will be terminated; not the same, not allowing Return, Goto, etc. Finally, I don't know why

12, sub-bag and periphery

Different from Java, the child can access the perimeter, do not use using, convenient, but reasonable? 13, attribute authority

Get / set does not set different permissions, for example, I want to INTERNAL SET, and public get, I don't know how to get 14, serialization

When most of the domains can use default serialization, a small number requires special processing, the few domains make the INTERNAL independent object, which customizes the serialization, and then contains this independent object in the original object.

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

Int [] is two-dimensional, matrix int [] [] is actually serrated

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-dimensional array of three-dimensional arrays of the INT type.

Array calls are clearly unspecified to the array of numeric types, for example, there is no conversion that can allow int [] as Object []

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

16. Is there any meaning?

Class a {public virtual void f () {console.writeline ("af);}} Class B: a {public override void f () {console.writeline (" bf ");}} Class C: b {New Public Virtual Void f () {console.writeLine ("cf");}} class d: c {public override void f () {console.writeLine ("df");}}}} Class test {static void main () { D = new d (); a a = d; b = d; c c = d; AF (); bf (); cf (); df ();}}} class C and D include two have the same Signature virtual method: An introduced by A is introduced by C. The method introduced by C hides the method of inheritance from a. Thus, the coverage declaration in D covers the method being introduced by C, and D covers the method introduced by A. It is impossible. Example produces the following output: b.fb.fd.fd.f

Under what reasons, do we need new virtual?

17. If you can't change the access modification when override

Even relaxation

18, X = Y results may be void

"In a form of x = y or x - = y, when X is an event member and references the result of the type of time occurs outside the type, the result of the operation is Void; this rule is forbidden to directly check the event member "? ? ?

19, constructor execution order

An instance variable initialization function can be initialized and a constructor initialization function, as a first statement that is automatically inserted in the main body of the constructor. Example Class a {int x = 1, y = -1, count; public a () {count = 0;} public a (int N) {count = n;}} Class B: a {double sqrt2 = math.sqrt (2.0); arraylist (100); int max; public b (): this (100) {items.add ("default");} public b (int N): base (n - 1) { MAX = n;}} contains many variable initialization functions, and also includes constructor initialization functions for each form (BASE and THIS). This example is related to the example described below, where each comment indicates an automatic inserted statement (automatically inserting the syntax used by the constructor call is not valid, at least to demonstrate this mechanism). Class a {int x, y, count; public a () {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: a {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 variable initialization function The converted statement is converted to an assignment statement, and that assignment statement is executed before the function of the base class constructor. This order ensures all instance fields in any accessed instance statement

Before execution, they are initialized by their variable initialization functions. For example: Class a {public a () {printfields ();} public virtual void printfields () {}} Class B: a {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 an instance of B, the following output is generated: X = 1, y = 0 Since 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 the value until the base class constructor returns to be executed. The static constructor is automatically called and cannot be explicitly called. Although many constraints are provided, the exact time and order executed by the static constructor is uncertain: a static constructor of a class is executed before any instance of this class. A class static constructor is executed before any static member of the class. A static constructor of a class is executed after the static constructor of all of its derived class. A static constructor of a class will never be executed more than one.

20, Struct can implement the interface

Slightly unexpected, it seems that Struct is basically similar to "Sealed classes for the basic data type field", I feel that I feel awkward after saying this sentence.

21, interface

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

22, enumeration

I don't understand why I have to talk to the integer, for & | Even so, it can be implemented internally, it is not necessary to expose it.

23, DELEGATE

Observer mode is supported in the language level, better than Java.util.observable, delegate is type security, different from java.util.observable, delegate is not unusual security

24, conditional properties

By marking the caller, instead of the traditional calling party conditions, the code is neat, single switch, and eliminate the disadvantages of macro

(The Java Programming Language Notes)

转载请注明原文地址:https://www.9cbs.com/read-14432.html

New Post(0)