0% found this document useful (0 votes)
9 views

The 'This' Reference Variable - Learn Object-Oriented Programming in C#

This document provides an introduction to the 'this' reference variable in object-oriented programming in C#. It explains that the 'this' variable refers to the current instance of a class and can be used to access members of the current object. It demonstrates using 'this' to differentiate between class fields and method parameters that have the same name, avoiding potential errors or confusion. The document states that in the next lesson, fields will be declared efficiently and manipulated using methods.

Uploaded by

Naj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

The 'This' Reference Variable - Learn Object-Oriented Programming in C#

This document provides an introduction to the 'this' reference variable in object-oriented programming in C#. It explains that the 'this' variable refers to the current instance of a class and can be used to access members of the current object. It demonstrates using 'this' to differentiate between class fields and method parameters that have the same name, avoiding potential errors or confusion. The document states that in the next lesson, fields will be declared efficiently and manipulated using methods.

Uploaded by

Naj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Back To Module Home

Object-oriented Programming
in C#

13% completed

Search Module

Introduction to Object-
Oriented Programming
in C#

Classes and Objects

Introduction to Objects and Classes


Got any feedback? Get in touch with us.
Declaration and Implementation

Accessing the Class Members

Access Modifiers

Fields

Methods

Static and Non-Static Methods

The 'this' Reference Variable


The 'this' Reference Variable
In this lesson, you will learn about the 'this' reference variable.

We'll cover the following

• The this Reference Variable


• Accessing a Field

The this Reference Variable


The this reference variable exists for every class. It refers to the current instance of
a class. The this.memberName specifies that we are accessing the memberName of the
current object.

Accessing a Field
Got any feedback? Get in touch with us.
We can use the this when we have a method argument which has the same name as
a field. It’s always a good convention for the beginners to use the this keyword in
their class implementation when initializing or accessing the fields. This will help us
avoid any confusion or errors.

Let’s see it in action:

1 class VendingMachine {
2
3 private int moneyCollected = 70;
4
5 // A simple print function
6 public void PrintMoney(int moneyCollected){
7 Console.WriteLine("Money Collected using this variable: " + this.moneyCollected);
8 Console.WriteLine("Money Collected without using this variable: " + moneyCollected);
9 }
10
11 }
12
13 class Demo {
14
15 public static void Main(string[] args) {
16 //passing the parameters
17 var vendingMachine = new VendingMachine(); // Object created with parameterized const
18 vendingMachine.PrintMoney(-10);
19 }
20
21 }

Run Save Reset

In the above code, we have used the this keyword on line 7. The purpose of using
Got any feedback? Get in touch with us.
this here is to differentiate between the arguments being passed to the method and

the fields of the class. For example, this.moneyCollected means we are referring to
the field of the class while simply using moneyCollected means that we are referring
to the argument being passed to the method.

At this point, we know all about the fields and methods of a class. In the next lesson,
we will discover an efficient way of declaring fields and how to manipulate these
fields using specific methods.

Back Mark As Completed Next

Static and Non-Static Methods Getters, Setters and Properties

Got any feedback? Get in touch with us.

You might also like