[.net] Property naming conventions [solved]

Started by
5 comments, last by Cryogenic 15 years, 3 months ago
I just started learning C# a few days ago and I'm a bit confused about naming my properties when they have class types. With get and set accessors from C++, I never had this trouble.

class MyClassWithAnImage
{
    public Image Image // <-- what to name this?
    {
        get { return _image; }
        set { _image = value; }
    }
    private Image _image;
}
GetImage and SetImage don't have this issue. Calling it Picture or Bitmap or something seems like a cop-out. Lower-case image doesn't match other .NET stuff. [Edited by - jdindia on January 21, 2009 7:50:03 PM]
Advertisement
You can call it Image. The C# is smart enough to know when you want the type and when you want the property.
Mike Popoloski | Journal | SlimDX
Uhh, I didn't expect that answer. Thank you sir. It helps.
While I realise that you can do this, you really shouldn't. Such a thing would be a nightmare for another program to read. Rather, give it a name that describes what the image is supposed to be. If you must keep things simple, then use a term that is the equivolent ie Picture instead of Image.

Granted, that's not much better, but I'm just trying to work within the example.
But sometimes Image really is the best name, in which case it's perfectly fine to use it. The .NET framework has many examples of this, such as SolidBrush.Color.

I never open up the namespaces so, in your case, my code would look like:
class MyClassWithAnImage{    public System.Drawing.Image Image    {        get { return _Image; }        set { _Image = value; }    }    private System.Drawing.Image _Image;}

Much easier for an external reader, as he won't have to guess if Image (the class) is the default System.Drawing.Image or some user-defined class.
----------------------------------------MagosX.com
I suggest calling it something that suggests the role of the image for example.

BackgroundImage (if the image is the background image)

ImageGlyph (if the image is some sort of decoration ...)

or if the class is supposed to be an image wrapper of some sort you can call it

SourceImage

or if the image is both a source and a destination (meaning it is operated upon/changed) then call it

ImageBuffer or simply Canvas

but in the end Image is good also if the role of the property is truly generic.

This topic is closed to new replies.

Advertisement