Usage of "this" keyword

Posted by balajirnaukri-12656 under ASP.NET on 12/28/2011 | Points: 10 | Views : 6896 | Status : [Member] | Replies : 7
Van anyone explain what is the use of "this" keyword and where should we use it.With some brief example

Thanks and Regards
Balaji.R
ASP.NET Devloper
Solve-IT corp



Responses

Posted by: SheoNarayan on: 12/28/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
this refers to the current object.

Read this code snippet http://www.dotnetfunda.com/codes/code363-use-of-this-keyword-in-csharp.aspx.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 6/12/2012 [Member] Bronze | Points: 25
Posted by: CGN007 on: 6/12/2012 [Member] Silver | Points: 25

Up
0
Down
When a method is called,
it is automatically passed a reference to the invoking object (that is, the object on which the
method is called). This reference is called this. Therefore, this refers to the object on which
the method is acting.

Also see this link
http://www.c-sharpcorner.com/uploadfile/puranindia/this-keyword-in-C-Sharp/

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Denny on: 6/12/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
find this code to understand
class A
{
int a=10;
void show(int a)
{
this.a=a;
}
}
here this.a gives the value of a as 10

Regards,
Denny Kurian

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/23/2012 [Member] Silver | Points: 25

Up
0
Down
Mark as Answer if its helpful to you,that motivates...

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muralidosscm on: 6/25/2012 [Member] Starter | Points: 25

Up
0
Down
The this keyword refers to the current instance of the class. Static member functions do not have a this pointer. The this keyword can be used to access members from within constructors, instance methods, and instance accessors.

The following are common uses of this:

To qualify members hidden by similar names, for example:
public Employee(string name, string alias)
{
this.name = name;
this.alias = alias;
}
To pass an object as a parameter to other methods, for example:
CalcTax(this);
To declare indexers, for example:
public int this [int param]
{
get
{
return array[param];
}
set
{
array[param] = value;
}
}
It is an error to refer to this in a static method, static property accessor, or variable initializer of a field declaration.

Example

In this example, this is used to qualify the Employee class members, name and alias, which are hidden by similar names. It is also used to pass an object to the method CalcTax, which belongs to another class.

// keywords_this.cs
// this example
using System;
public class Employee
{
public string name;
public string alias;
public decimal salary = 3000.00m;

// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}

// Printing method:
public void printEmployee()
{
Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
// Passing the object to the CalcTax method by using this:
Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
}
}
public class Tax
{
public static decimal CalcTax(Employee E)
{
return (0.08m*(E.salary));
}
}

public class MainClass
{
public static void Main()
{
// Create objects:
Employee E1 = new Employee ("John M. Trainer", "jtrainer");

// Display results:
E1.printEmployee();
}
}
Output

Name: John M. Trainer
Alias: jtrainer
Taxes: $240.00


Regards
Muralidoss M

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajkatie on: 6/25/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Kindly refer following link
http://msdn.microsoft.com/en-us/library/dk1507sz%28v=vs.71%29.aspx

Rajesh Patel
.Net Trainer(MSCD | MCT | MS)
Brainbench (Asp.Net 4.5 and C# 5.0)

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response