This keyword is used in C # programming very frequently, today this article on the use of this keyword to do some analysis, I hope to mention to everyone's C # program design has a certain help role. The specific analysis is as follows:
1. Representing the current class
, you can use this in the current class to access the current class member variables and methods (note that this is not used in static methods), and can also be used for parameter passing to pass a reference to the current object.
The sample code is as follows:
Class program{ static void Main (string[] args) { ThisClass testobj = new ThisClass (); Console.ReadLine (); }} Class thisclass{ private string A {get; set;} Public ThisClass () {/ * Current Class This access class in property A static method cannot access a property */this . A = "Test String"; Console.WriteLine (this. Testfun ("Testfun:")); } private string Testfun (string args) { return args + this. A; }}
The results of the operation are as follows:
2. Declaring indexers
Indexers: Allow instances of classes and structs to be indexed in the same way as arrays, with indexers similar to attributes, except that their accessors take parameters, which are known as parameter attributes, which can be overloaded, belong to instance members, and cannot be declared static.
The sample code is as follows:
Class program{static void Main (string[] args) {Indexclass intindexclass = new Indexclass (); Intindexclass[0] = new ThisClass ("Intindexclass 111"); INTINDEXCLASS[1] = new ThisClass ("Intindexclass 222"); Indexclass Stringindexclass = new Indexclass (); stringindexclass["string1"] = new ThisClass ("Stringindexclass string1"); stringindexclass["string2"] = new ThisClass ("Stringindexclass string2"); Console.ReadLine (); }}class indexclass{/* Declaration attribute */Private thisclass[] Thisclassarr = new THISCLASS[10]; Private Hashtable Thisclassstrarr = new Hashtable (); /* Create indexer 1 indexes can be overloaded, belong to instance members, cannot be declared as static*/public ThisClass This[int index] {get {return thisclassarr[index];} set {This.thisclassarr[index] = value;} }/* Create indexer 2*/public ThisClass this[string index] {get {return thisclassstrarr[index] as ThisClass; } set {This.thisclassstrarr[index] = value;} }}class thisclass{private string A {get; set;} Public ThisClass (String str) {/*The current class of this access class in the property a static method cannot access the A property */this. A = str; Console.WriteLine (this. Testfun ("Testfun:")); } private String Testfun (String args) {return args + this. A }}
The results of the operation are as follows:
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Use case analysis of this in C #
This address: http://www.paobuke.com/develop/c-develop/pbk23624.html
Related content C # request HTTP to send a Web page a way to receive data Windows C # Timing Manager Framework task.mainform a meta-group instance of the new features of C # syntax in detail in the general scenario of C # list implementing row-to-column
23 Design Patterns in Simplicity C # using Microsoft's own library to convert between Chinese traditional and simplified to achieve simple pseudo-time synchronization in unity the role and usage of dictionary in C #
Use case analysis of this in C #