How to implement a non-static method in the study of Delegates

Source: Internet
Author: User

In C # for delegates how to load non-static methods have been very puzzled, their own understanding of non-static methods, if you want to implement a non-static method of security must refer to the instance of the
field, after consulting the data, know that the delegate class has a _target field if the delegate is a static method with a value of zero, if the delegate is non-static instance that will automatically find the method,
It is very vague, this _target field should be an object type, but what the inside address points to. I'm going to declare a class with two fields on the program. One of the two fields is static
A non-static, there are two methods a static one non-static, I in the method to face the method of the class to operate, to see what impact on the instance.

 Public Delegate voidtestchangedelegate ();  Public classTestchange { Public intNotstaticint;  Public Static intStaticint;  Public Static voidStaticinway () {textstruct TT=Newtextstruct (); Console.WriteLine ("static method before {0}", Staticint); Staticint+=1; Console.WriteLine ("static method after {0}", Staticint); }         Public voidNotstaticway () {textstruct TT=Newtextstruct (); Console.WriteLine ("non-static method before {0}", Notstaticint); Notstaticint+=1; Console.WriteLine ("non-static method after {0}", Notstaticint); }    }
View Code

And then write down the main function.

Static void Main (string[] args)        {            // Delegate method use changes the value of the original instance test             new  Testchange ();             New testchangedelegate (tt.notstaticway);             + = Tt.notstaticway            ; + = Testchange.staticinway            ; + = Testchange.staticinway;            TTC ();            Console.WriteLine (tt.notstaticint);            Console.WriteLine (Testchange.staticint);}
View Code

The output result is

static method before 0 static method after 1 static method before 1 static method after 2 non-static method before 0 non-static method after 1 non-static method before 1 non-static method 2 non-static field is 2 static field 2

  

From the result, if the non-static method, the instance in _target is the instance in the original Declaration class
But when I declare a class as a struct (that is, change the public class Testchange to public struct testchange), I run the program again.
The output result is

Non-static method before 0 non-static method after 1 non-static method before 0 non-static method after 1 static method before 0 static method after 1 static method before 1-static method 2 non-static field is 0 static field 2

The same structure instance but the instance field is not changed by non-static method, but changed at run time, it appears that the instance address in _target is not the original struct TT,
The TT should be copied and then boxed into an object to assign T address to _target, but there are three cases of replication, one is shallow copy, one is deep copy, the other is normal copy
What kind of a direct address? (for shallow and deep copy references ...) )
First think of the above results if it is a deep copy of the above whether the class or the structure of the results are the same, now is to determine whether to directly assign the original object address of the normal copy or shallow copy
So I modified the Testchange class to modify it so that I added a struct to the class, if the static and non-static fields before and after the normal copy struct are the same as the outside class, if it is shallow
If you copy it, the result is the same as the structure Testchange.

 Public Delegate voidtestchangedelegate ();  Public classTestchange { Public intNotstaticint;  Public Static intStaticint;  Public structtextstruct { Public intNotstaticint;  Public Static intStaticint; }         Public Static voidStaticinway () {textstruct TT=Newtextstruct (); Console.WriteLine ("Method {0} {1} before", Staticint,tt.notstaticint); Staticint+=1; Tt.notstaticint+=1; Console.WriteLine ("Method {0} {1} after", Staticint, Tt.notstaticint); }         Public voidNotstaticway () {textstruct TT=Newtextstruct (); Console.WriteLine ("Method {0} {1} before", Notstaticint,tt.notstaticint); Notstaticint+=1; Tt.notstaticint+=1; Console.WriteLine ("Method {0} {1} after", Notstaticint, Tt.notstaticint); }}} Testchange TT=NewTestchange (); Testchangedelegate TTC=Newtestchangedelegate (Tt.notstaticway); TTC+=Tt.notstaticway; TTC+=Testchange.staticinway; TTC+=Testchange.staticinway;            TTC ();            Console.WriteLine (Tt.notstaticint);            Console.WriteLine (Testchange.staticint);            Console.WriteLine (TT.TT.NOTSTATICINT); Console.WriteLine (testChange.textStruct.staticInt);
View Code

The result is

11 0 101   1 2 2 2 0 2


After changing a class to a structure
The result is

11 0 101   1 2 0 2 0 2

The results show that the result of the structure TT is the same as the above structure testchange, and the analysis shows that it is shallow copy.


It seems that the. NET Framework uses shallow replication instead of directly referencing addresses in the background to copy objects to instance methods.

How to implement a non-static method in the study of Delegates

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.