C # -- this keyword (1 ),

Source: Internet
Author: User

C # -- this keyword (1 ),

// My C # is followed by Meng Ge (Liu tiemeng) (my formal teacher), and I learned "C # getting started with languages, I also explained some things I don't know. It's a huge fortune and a rare mentor!

 

When I was learning C #, the teacher used the this keyword in some examples. Meng gave a slight explanation in the video and did not explain it in depth, I still have questions on this part: how can I use this keyword?

So I looked for some information this afternoon and did not know whether my understanding was correct. I hope you can give me some guidance.

 

Starting fromMicrosoft's official C # programming guideExcerpt section:

The following are common uses of this:

  • Restrict hidden members with similar names

  • Passing objects as parameters to other methods

  • Declare Indexer

---------------------------------------------------------

For the first purposeRestrict hidden members with similar namesI have just got a clue that my language organization capability is poor. Let's take a look at it through an example:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace @this{    class Program    {        static void Main(string[] args)        {            Student stu = new Student();            stu.GetMessage("Mark", 1);            Console.WriteLine("My name is {0}.My ID number is {1}.",stu.Name,stu.ID);        }    }    class Student    {        public string  Name { get; set; }        public int ID { get; set; }        public void GetMessage(string Name,int ID)        {            this.Name = Name;            this.ID = ID;        }    }}

  

This. Name = Name; // The Name here is the Name passed in.

This. ID = ID; // The ID here is the uploaded ID.

This refers to an instance created in the Student class (or replaced by this)

Let's change the code:

public void GetMessage(string Name,int ID)        {            Student stu = new Student();            stu.Name = Name;            stu.ID = ID;        }

We created an instance named stu in this class and assigned a value for "himself" through it. After running, the program is exactly the same.

Bytes -----------------------------------------------------------------------------------------

Make a summary of the first purpose:

To put it bluntly, we can think that this creates an instance for us and then uses this instance for a series of operations.

Bytes ------------------------------------------------------------------------------------------

To be Continued!

Bytes ------------------------------------------------------------------------------------------

I hope that the majority of users will point out the problem, point out where I have understood the mistake, communicate with each other, and make progress together!

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.