Solved

A question about C# style.

Posted on 2003-10-24
10
287 Views
Last Modified: 2010-04-16
OK, I know there are probably as many answers to this question as there are developers, but I'm going to ask anyway.

In looking at a lot of C# sample code I've seen some things that I have questions about and would just like to get people's thoughts:

1.)  Why include the this keyword when accessing class members?  I've seen code where every access of a member includes this.
2.)  What do people think about using the entire namespace name to define an object versus using a short form of the namespace based on a "using" directive?

Thanks in advance.
0
Comment
Question by:derekl
10 Comments
 
LVL 2

Accepted Solution

by:
smitty22 earned 25 total points
ID: 9617644
1)  The code was most likely developed in Visual Studio, where using "this" allows the developer to take advantage of intellisense, which results in less keystrokes.  It may look like more, but it's usually less in most cases.
0
 

Assisted Solution

by:LanceWLarsen
LanceWLarsen earned 25 total points
ID: 9617675
On question #1 -- As someone who does not include the "this" keyword in the situation that you described in your question -- as in using "this.foo" when using "foo" works -- I can only speculate that it is done for coding clairity, i.e. you know you're referring to the specific class in which you are within context.  To the best of my knowledge (and anyone who knows otherwise please chime in) this does not impact the compiler or in any way optimize compilation.

On question #2 -- As to fully qualifying the name space versus utilizing a "using" directive -- I very much do this in the cases where I'm only using a method in a few places -- this certainly clairfies what you're calling and allows for fewer searches for which method belongs to which "using" directive when using CPOOP (Cut-n-Pase Object Oriented Programing <g>) coding techniques.

Regards...

...Lance
0
 

Expert Comment

by:LanceWLarsen
ID: 9617688
"smitty22" has a good point on question #1 -- I'm more of a <CTRL-J> kind of coder
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9618509
I would like to start saying that its all a matter of coding style, so lets go step by step

1.)  Why include the this keyword when accessing class members?  I've seen code where every access of a member includes this.

The idea to use the this keyword is just to make clear that you're refering to the member or method of the class.
It's just in order to make it easier to know what you're talking about........

Just imagine a big code, where the this keyword is not used........ and in the middle you have something like

list = new ArrayList();        

you don't know if the list is a local variable of the method, or a class member.

Apart from that it is also used when people define their member without any particular notation and they may get confused with the method paramters.

For example:

public class Point3D
{
   public Point3D( float x, float y, float z )
   {
       this.x = x;          // In this case, the this keyword allows you to use the same names without any problem
       this.y = y;
       this.z = z;
   }

   private float x;
   private float y;
   private float z;
}



2.)  What do people think about using the entire namespace name to define an object versus using a short form of the namespace based on a "using" directive?

Its almost the same that in the previous point

just to make sure that you're refering to the right class

suppose you have the Foo class in the Graphic namespace and the Foo class in the Model namespace

if you have

using namespace Graphic;
using namespace Model;

// Code

and then you do

Foo foo = new Foo();           // What class are you refering to?

However, I do not use this style. I'd rather use the using namespaces directives and try to avoid repeating the class names.
But then again, it's just a matter of coding style.

Hope it helps

Tincho

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:zrh
ID: 9618671
Ditto to what tincho said...
ZRH
0
 
LVL 1

Expert Comment

by:kellycoinguy
ID: 9625329
While the example above works... what happens when some greenhorn comes along with an eye to optimizing and does this not knowing the ramifications of his actions:

public class Point3D
{
   public Point3D( float x, float y, float z )
   {
       x = x;
       y = y;
       z = z;
   }

   private float x;
   private float y;
   private float z;
}

I prefer a very light kind of hungarian notation to keep things straight.

public class Point3D
{
   public Point3D( float pX, float pY, float pZ )
   {
       x = pX;
       y = pY;
       z = pZ;
   }

   private float x;
   private float y;
   private float z;
}

Although in reality I'd probably make all three floats properties as well.

I once asked people if they rolled toilet paper from the top or bottom? It all gets the same result, but religious wars have been fought over less controversial subjects. I guess my only real point is don't do something that could confuse someone who knows less about the language than you do, unless it's for a very good reason. Our job isn't to prove that we know more about the language than our coworkers, it's to create code that is both elegant and clear. Also, you may have local standards at your workplace that you'll want to comply with.

-Kelly
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9626371
Sorry kelly, I understand what you're talking about, but I was just answering a question

>> 1.)  Why include the this keyword when accessing class members?  I've seen code where every access of a member includes this.

Although I would also write code like you did, that way of coding wasn't illustrative for the reason of why people sometimes used the this keyword.

My explanation was based on the exampled I provided. I never said that that was what I suggested and in any case I wanted to prove I know more about the language than anybody.

Tincho
0
 
LVL 9

Expert Comment

by:tinchos
ID: 11697509
Being this one just a 50 point questions I agree with it

Tincho
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get String split 5 46
Programmatically signing Word macros 4 61
Why Does This Page Not Show Up? 4 22
User Authentication using Digital Certificate 2 21
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now