473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

csharp concepts 101

I'm in the early stages of learning csharp. I have an example class, and
I'm trying to understand it clearly. I've included comments and questions.
Any clarification would be greatly appreciated.

class Point
{
// This is a constructor because it is named the
// same as the class? I'm also defining 2 parameters that
// can be passed to an instance of this class.
public Point(int x, int y)
{
this.x = x; //setting the value of the member fields
this.y = y; //to the values passed.
}

// Declaring member fields.
// Why do we declare them after they have
// been referenced in the constructor?
// Declaring member fields with the same name as the parameters
// seems odd -- seems like a bad idea?
public int x;
public int y;
}
Nov 16 '05 #1
6 1152
HolyCow <no****@nowhere.com> wrote:
I'm in the early stages of learning csharp. I have an example class, and
I'm trying to understand it clearly. I've included comments and questions.
Any clarification would be greatly appreciated.

class Point
{
// This is a constructor because it is named the
// same as the class?
Yes.
I'm also defining 2 parameters that
// can be passed to an instance of this class.
The parameters are passed to the constructor - you don't pass
parameters to an instance as such.
public Point(int x, int y)
{
this.x = x; //setting the value of the member fields
this.y = y; //to the values passed.
}

// Declaring member fields.
// Why do we declare them after they have
// been referenced in the constructor?
The textual location doesn't matter (in this context - there are a
*very* few places where the order matters).
// Declaring member fields with the same name as the parameters
// seems odd -- seems like a bad idea?
Some people think it is, some think it isn't. I use the convention
shown here, but others prefer to use _x or m_x.
public int x;
public int y;
}


--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
HolyCow <no****@nowhere.com> wrote:
// Declaring member fields with the same name as the parameters
// seems odd -- seems like a bad idea?


Some people think it is, some think it isn't. I use the convention
shown here, but others prefer to use _x or m_x.


Or, use the convention of prefixing all references to class members with
"this."

--Bob
Nov 16 '05 #3
HolyCow...The problem is that useful names are in short supply, so most
of
us want to reuse the same name for member fields and local fields. If
you
don't have a standardized method of differentiating between member
fields
and local fields you can get yourself into lots of hot water. The
standard C#
approach is to preface member fields with this within a method with a
potential name collision with a local field. The standard C++ approach
is to
simply prepend m_ to the member field name. Another approach would be to
prepend in to a parameter name such as inSomeString. so

this.x= x
m_x= x
SomeString= inSomeString

Regards,
Jeff
// Declaring member fields with the same name as the parameters
// seems odd -- seems like a bad idea?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Jeff Louie <je********@yahoo.com> wrote:
HolyCow...The problem is that useful names are in short supply, so
most of us want to reuse the same name for member fields and local
fields. If you don't have a standardized method of differentiating
between member fields and local fields you can get yourself into lots
of hot water.
So people keep saying, but I can't remember the last time it ever bit
me, whereas I *do* find prefixes lower the readability for me. If you
keep your methods short enough (which is good for other reasons), it
should be obvious which is which.
The standard C# approach is to preface member fields with this within
a method with a potential name collision with a local field.


No, there *is* no standard C# approach. That's *a* common approach, but
it's not standard by any means.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon... I am speaking from my personal experience. Once burned, a bit
more
careful. I do agree that the prefixes are bothersome, but I would
recommend
using m_x in a non trivial C++ project despite the lowered readabililty.

Regards,
Jeff
So people keep saying, but I can't remember the last time it ever bit

me, whereas I *do* find prefixes lower the readability for me. If you
keep your methods short enough (which is good for other reasons), it
should be obvious which is which.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Jeff Louie <je********@yahoo.com> wrote:
Jon... I am speaking from my personal experience. Once burned, a bit
more careful. I do agree that the prefixes are bothersome, but I
would recommend using m_x in a non trivial C++ project despite the
lowered readabililty.


In C++ it's a fairly standard convention, and I'd probably use it
there. It's less standard in C#, and having never had a problem, I'd
rather have higher readability. Each to their own though :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ravinder | last post by:
I wanted to develop a multithreaded program using OO concepts on windows platform. Problem: I have to simulate two layers(similar to TCP/IP stack layers), and the layer functionality is of finite...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
4
by: light_wt | last post by:
Hi I am taking the 2555 class and a lot of the material is over my head. I don't like the MS's book because there is no step-by-step on interacting with the VS.NET Is there good free resource...
7
by: Peter Smirnov | last post by:
Sorry for this newbie question but as far as I heard one need at least VisualStudio to develop CSharp applications. Is this correct? Are there otherwise some command line tools like javac.exe and...
2
by: news.microsoft.com | last post by:
Hi: I work in Csharp's parser files by LEX/YACC.Now I have only CSharp-lex.l and CSharp.y file,but they not have CSharp'comment Parse. this is a part of CSharp-lex.l. ........................
8
by: ABC | last post by:
In VB, we can write the share function as Public Shared Function FnXXX() .... .... End Function How about on C#?
3
by: Robert | last post by:
I've been working with ASP.NET for about a year and I think I'm on the verge of "getting it." I suspect that if I came to understand a few key concepts it would all come together. One of those "key...
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
1
by: Swathika | last post by:
Hi, Sometimes, you never get chance to learn 'Advanced Design Concepts and Real-time Scenarios' from institutes or through book-learning. But, in my blog, I have gathered some of the amazing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.