Use case analysis of this in C #

Source: Internet
Author: User

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 #

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.