The use and role of this in C #

Source: Internet
Author: User

In C #,

1. The This keyword represents the current instance, and we can use this to invoke the current instance's member methods, variables, attributes, fields, etc.

2. This can also be used as a parameter to the current instance as a parameter incoming method.

3. You can also declare indexers by this[]

Here is a program to show the use of this:

Introduction of mission space system using System;
Declaring a namespace Callconstructornamespace callconstructor {
Declaring class car

public class Car

{
A variable that is not declared with Static is called a non-static member
Class, we can only instantiate the class by invoking the constructor of the class to add "." to the resulting instance. To access

Intpetalcount = 0; Declare a non-static integer variable Petalcount in the car class with an initial value of 0
String s = "null"; Declares a non-static string variable s with an initial value of "null"; Note: s = "null" is different from s = null
Default constructor for car class

Car (Intpetals)

{
Petalcount = petals; The default constructor for the car class assigns a value of Petalcount to the parameter petals passed in

Console.WriteLine ("Constructorw/int arg only,petalcount =" + Petalcount); Output Petalcount}
Constructors for overloading car classes
: This (petals) means that the value of the petals variable is called from the current class as the constructor overload method

Car (string s, int petals) The second argument car (string s, int petals): this (petals)

{

/* Assign a value to s in the constructor. A non-static member can use this in a constructor or non-static method to invoke or access, or to directly hit the name of a variable, so this sentence is equivalent to S = s, but then you will send the variable s of the class with the passed parameter s with the same name, which will cause two definitions, So add this one. Indicates that the s to the left of the equals sign is the current class's own variable */

THIS.S = s;
Console.WriteLine ("String & int args");

}
Overloaded constructors,: This ("Hi", 47) represents the overloaded constructor of the car (Strings, int petals) and passes directly to the variable "hi" and 47
Car (): This ("Hi", 47)
{

Console.WriteLine ("Defaultconstructor"); } public static Voidmain () {Car x = Newcar ();

Console.read ();

}

The use and role 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.