Advertisement
//C# Example of this keyword
using System;
class A
{
//Defining the method Message()
public void Message()
{
Console.WriteLine("Hello from A");
//Printing this keyword prints the name of the class of the currently executing object.
Console.WriteLine(this);
}
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Calling the method Message() by using the object of class A
ob.Message();
}
}
Hello from A
A
//C# Example of this keyword
using System;
class A
{
//Instance variables
int a = 10;
char c = 'A';
float f = 12.3f;
//Defining the method Message()
public void Message()
{
Console.WriteLine("The value of the instance variables of the currently executing object: ");
//Using this keyword to access the instance variables of the currently executing object.
Console.WriteLine(this.a);
Console.WriteLine(this.c);
Console.WriteLine(this.f);
}
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Calling the method Message() by using the object of class A
ob.Message();
}
}
The value of the instance variables of the currently executing object:
10
A
12.3
Advertisement
//C# Example of this keyword
using System;
class A
{
//Defining the Message() message
public void Message()
{
//Using this keyword to call the Hello() method
//associated with the currently executing object.
this.Hello();
}
//Defining the Hello() method
public void Hello()
{
Console.WriteLine("Bonjour");
Console.WriteLine("Hello");
Console.WriteLine("Namaste");
}
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Calling the method Message() by using the object of class A
ob.Message();
}
}
Bonjour
Hello
Namaste
//C# Example of shadowing issue
using System;
class A
{
//Instance variable
int a = 20;
//Defining the method SetA()
//Which should update the value of the instance variable, a.
//But its local variable has the same name, a,
//As instance variable of the class A, so it won't happen
public void UpdateA(int a)
{
a = a;
}
//Defining the Main() method
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Accessing the value of instance variable, a, before calling the UpdateA() method
Console.WriteLine("Before calling UpdateA(), the value in instance variable, a: "+ ob.a);
//Calling the UpdateA() method and passing it 100
//to set the value of the instance variable, a
ob.UpdateA(100);
//Accessing the value of instance variable, a, after calling the UpdateA() method
Console.WriteLine("After calling UpdateA(), the value in instance variable, a: "+ ob.a);
}
}
Before calling UpdateA(), the value in instance variable, a: 20
After calling UpdateA(), the value in instance variable, a: 20
//C# Example of this keyword used to resolvd the shadowing issue
using System;
class A
{
//Instance variable
int a = 20;
//Defining the method SetA()
//Which should update the value of the instance variable, a.
//But its local variable has the same name, a,
//As instance variable of the class A, so it won't happen
public void UpdateA(int a)
{
//Accessing an instance variable with this keyword,
//to differ it from a local variable with the same name
this.a = a;
}
//Defining the Main() method
public static void Main(String[] ar)
{
//Creating an object of class A
A ob = new A();
//Accessing the value of instance variable, a, before calling the UpdateA() method
Console.WriteLine("Before calling UpdateA(), the value in instance variable, a: "+ ob.a);
//Calling the UpdateA() method and passing it 100
//to set the value of the instance variable, a
ob.UpdateA(100);
//Accessing the value of instance variable, a, after calling the UpdateA() method
Console.WriteLine("After calling UpdateA(), the value in instance variable, a: "+ ob.a);
}
}
Before calling UpdateA(), the value in instance variable, a: 20
After calling UpdateA(), the value in instance variable, a: 100
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement