A little >this< question

  • Thread starter Thread starter Jesper.
  • Start date Start date
J

Jesper.

Hi,

I'm a former C++ programmer, and normally I didn't see
the use of the this pointer (pointer in c++) used as
often as I see it in C#. When the VS generates code for
you it uses >this< all the time. Examples I study in
different newsgroups uses >this< all the time - like the
example below. Why is >this< used so extensively when it
is not required? (like in the example below) - if it is
required then why?

thanx Jesper, Denmark

public class EmployeeCollection : CollectionBase,
ICustomTypeDescriptor
{
#region collection impl

/// <summary>
/// Adds an employee object to the
collection
/// </summary>
/// <param name="emp"></param>
public void Add( Employee emp )
{
this.List.Add( emp );

}
 
A) it's easier to use intellisense and choose from a list.

B) it's much clearer that you are talking about a member in "this" instance
of the current class
 
Another benefit of writing "this" explicitly is that it reduces the chance
for accidental name capturing.

For example, if you have a member variable FooM and a non-member variable
FooNM both in scope, and you intend to refer to FooM, but you write FooNM
by mistake. In this case, the compiler won't be able to help you (assuming
FooM and FooNM have the same type). However, if you always write "this."
with member variables, you would've written "this.FooNM", which will be
caught by the compiler.

- Zhanyong Wan

Visual Studio and .NET Setup

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included samples (if any) is subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

--------------------
| From: "Michael Mayer" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: A little >this< question
| Date: Sat, 23 Aug 2003 13:35:10 -0500
| Lines: 53
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: c66.169.142.78.ts46v-08.otn-c2.ftwrth.tx.charter.com
66.169.142.78
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:178852
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I use "this' for the exact two reasons Frank mentioned. If you don't
| preface member variables with "this.", then you generally end up using
some
| other naming convention such as of m_var myVar or _var for the members of
a
| class to differentiate from locals, etc. I prefer this. to those others
for
| the added benefit of intellisense (otherwise, I'm likely to misspell
and/or
| completely forget the names of my class-wide variables).
|
| mike
|
| | > A) it's easier to use intellisense and choose from a list.
| >
| > B) it's much clearer that you are talking about a member in "this"
| instance
| > of the current class
| >
| >
| > | > > Hi,
| > >
| > > I'm a former C++ programmer, and normally I didn't see
| > > the use of the this pointer (pointer in c++) used as
| > > often as I see it in C#. When the VS generates code for
| > > you it uses >this< all the time. Examples I study in
| > > different newsgroups uses >this< all the time - like the
| > > example below. Why is >this< used so extensively when it
| > > is not required? (like in the example below) - if it is
| > > required then why?
| > >
| > > thanx Jesper, Denmark
| > >
| > > public class EmployeeCollection : CollectionBase,
| > > ICustomTypeDescriptor
| > > {
| > > #region collection impl
| > >
| > > /// <summary>
| > > /// Adds an employee object to the
| > > collection
| > > /// </summary>
| > > /// <param name="emp"></param>
| > > public void Add( Employee emp )
| > > {
| > > this.List.Add( emp );
| > >
| > > }
| > >
| >
| >
|
|
|
 
Back
Top