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 #