INSIDE C # 2nd Edition side view (2)

xiaoxiao2021-03-05  24

Chapter 4 miscellaneous

1.ref, out, reason, value type, is good

Both the parameters called the function, first is Value Type, if there is no modification by any modification, then the value is passed, if the reflected parameters seem to be the pointer in C , it is also a problem, but worth paying attention to the problem Yes, if a certain parameter of the function is modified by REF, the parameter must be assigned, if it is OUT modification, there is no limit, but OUT modified parameters must be assigned inside the function body.

Next, Reference Type, in the absence of modification, the performance of Reference Type and the performance of REF VALUE TYPE, who makes people are refrescence. Look below, the following functions, only one of the following functions, only one can be compiled through the Class SomeClass {// this Won't compile public anotherclass changeObject (out noidherclass ref1) {ref1.id = ref1.id * 2; return Ref1; RETURN REF1; } // This will not compile public AnotherClass ChangeObject (out AnotherClass ref1) {ref1.ID = 4; return ref1;} // This will not compile public AnotherClass ChangeObject (out AnotherClass ref1) {int x = ref1.ID; Ref1 = new annotherclass (); ref1.id = x * 2; return ref1;}

// this will Compile Public AnotherClass ChangeObject (Out Anotherclass Ref1) {Ref1 = New AnotherClass (); Ref1.id = 99; Return Ref1;}} The reason is that OUT requires this parameter to assign a value inside the function body, the first function is not Assigning, compiling does not pass the second function, the same first; the third function, although New, New's place is wrong, if the first NEW assigns no problem, just like the last one.

Add out has been so depressed, to see how ref plus the effect it functions look class AnotherClass {public int ID;} class SomeClass {// public AnotherClass ChangeObject (AnotherClass ref1) public AnotherClass ChangeObject (ref AnotherClass ref1) {ref1 .Id = ref1.id * 2; return ref1;}}}

Here, it adds a ref and the same effect. In the end, it will change the value of the ID in the call function and then look at the following Class Anotherclass {public int id;} class someclass {public anotherclass changeObject (ref1.id = ref1) {ref1.id = ref1.id * 2; return ref1;} Public void subtlechange (ref1 = new annotherclass (); ref2.id = 999;}}

The first REF1 is modified by REF, so when the Ref1 is re-NEW, the original value in the call function is also redistributed, but the REF2 is not modified, so it is a value pass, the ref2 is re-reported in the function. Allocation, and the original value of the calling function has not changed, just like a pointer, then the pointer is re-pointing to the new place, and the original pointer in the call function refers to the old place, and there is no data pointed to the original pointer inside the function. Do any changes, the result of the main function call below is: Class ValReftest {Static Void Main (String [] args) {SomeClass sc = new someclass (); anotherclass ref1 = new anotherclass (); ref1.id = 3; anotherclass Ref2 = sc.changeObject (ref1); console.writeline ("Ref1.id = {0}, ref2.id = {1}", ref1.id, ref2.id);

Sc.SUBTLECHANGE (Ref Ref1, Ref2); console.writeLine ("Ref1.id = {0}, ref2.id = {1}", ref1.id, ref2.id);}}}}}}} 6, ref2.id = 6Ref1.id = 999, ref2.id = 6

2. Function overload Overloading

It is worth mentioning that there is one, about REF and OUT. For two functions, the compiler considers that the function that does not add REF or OUT can be overloaded with the function of the REF or OUT, but the two functions of the REF or OUT are not available.

Function 1: Protected void Writentry (string entry) {console.writeline (entry);}

Function 2: Public void Writentry (Ref string entry) {console.writeLine (entry);}

Function 3: Public void Writentry (out string entry) {entry = "foo"; console.writeLine (entry);}

The function is one and two or three constitute an overload, but two, three cannot exist at the same time.

3. The difference betweenounceriding and overloading

One has always been a clear question, today I think it is searching. It turns out that there is such a brilliant explanation. It is a different manifestation of Overriding and overloading Overloading in the Hate. Overriding Overriding is a manifestation of polymorphism between parent class and subclasses, and overloading overloading is a manifestation in a class. If a method is defined in the subclass with the same name and parameters as its parent class, we say that this method is overriddled. When the subject of subclass uses this method, the definition of the subclass will be called. For it, the definition in the parent class is like "shielded". If a plurality of the same names are defined in a class, they or have different parameters or different parameter types, called overloading.

4.Virtual, Override, New Several Keywords

Lifting a small example class a {public void f (INT i) {m_i = i * i;} protected int m_i;}

Class B: a {public new void f (int i) {m_i = i;}}

When you need to rewrite a function in the base class in the inheritance class, you need to use the New keyword, otherwise compile errors. At this time, B b = new b (); bf (100); calling B :: f (), a a = new b (); AF (); calling A:: F (), there is no existence Any polymorphic concept is just a function of the overlay process, which is the function of who is called,

But after joining Virtual, the concept of polymorphism

Class A {public Virtual Void F (INT i) {m_i = i * i;} protected int m_i;}

Class B: a {public override void f (INT i) {m_i = i;}}

Note that the Virtual key is added before F. Basically the same as the virtual function concept in C , just in the inheritance class, you need to add Override this keyword at this time, B b = new b (); bf (100); call is b: f (), this is no Doubt, don't call it? A a = new b (); AF (); call is also b: f (), this is a polymorphic problem, a lot of things related to the virtual function table need to mention In

See the explanation of the virtual function in C

http://blog.9cbs.net/zdliang/archive/2005/04/15/348772.aspx

5. Static function

Attention The scope of the static function can be accessed in the class, and the static function can only access static variables in the class, and the member function can access static variables and member variables but this cannot access static variables.

The static constructor does not have parameters, and cannot be modified by PUBLIC, and the static constructor is called before the constructor. Look at the code below

Class SomeClass {public static int x;

Static SomeClass () {x = 1; console.writeline ("someclass initialized");

Public SomeClass () {Console.WriteLine ("Non-Static Ctor");

Public static void foo () {console.writeLine ("foo");}}

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

New Post(0)