By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,490 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,490 IT Pros & Developers. It's quick & easy.

getter and setter methods in C#?

P: n/a
Hello,

I´m coming from the Java World. Here Programmers often use (like in
C++?) getter and setter methods.

F.e.:

class Mirror{

private int width_;
private int height_;

public Mirror(width, height)
{
width_ = width;
height_ = height;
}

public int getWidth()
{
return width_;
}
public int getHeight()
{
return height_;
}
}
In C# I often have seen now, that programmers do not use getter and
setter methods.

The define their class variables as public like:
class Mirror{

public int width_;
public int height_;

public Mirror(width, height)
{
width_ = width;
height_ = height;
}
}
Why do they do that? Only to have fewer methods?
For me it makes sense to have getter and setter methods to encapsulate
the classes so that no class can change the instance variables of
another class.

Except of course if they use the setter-methods.

Regards,
Martin
Nov 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Don't know what gave you the idea, but in any case C# supports
getter/setter methods in properties in a very nice way, which is also
followed by the studio intellisense helper thingie:

-----------------------------------------------
class SomeThing
{
// Private fields to hold the variable.
private string myField;
private string myOtherField = "foo";

public string MyField
{
get
{
// Do some custom stuff here, if desired.
return this.myField;
}
set
{
// Do some custom stuff here, if needed.
this.myField = value;
}
}

public string MyOtherField
{
// Only declare readonly property by omitting 'set'.
get
{
return this.myOtherField;
}
}
}
-----------------------------------

Regards,
Jeroen

Nov 6 '06 #2

P: n/a
Public fields are not encouraged; I suspect this relates more to the
developers who have written the code you have been looking at.

See various debates oln this very forum, generally strongly in favor of
properties as per your example. You do get exceptions though...

Marc
Nov 6 '06 #3

P: n/a

"Martin Ppping" wrote:
>
I´m coming from the Java World. Here Programmers often use
(like in C++?) getter and setter methods.
C# programmers use it equally much, or maybe even more frequently than I
have seen Java programmers do.
F.e.:

class Mirror{

private int width_;
private int height_;

public Mirror(width, height)
{
width_ = width;
height_ = height;
}

public int getWidth()
{
return width_;
}
public int getHeight()
{
return height_;
}
}
In C# I often have seen now, that programmers do not use getter
and setter methods.
[snip]
Why do they do that? Only to have fewer methods?
For me it makes sense to have getter and setter methods to
encapsulate the classes so that no class can change the
instance variables of another class.

Except of course if they use the setter-methods.
The examples you have encountered are not examples on how to do it in C# or
any other language. It's in most cases probably a question of lazy
programmers.

I started with saying that it's even possible that C# programmers use
accessors/mutators more frequently than Java programmers do, as C# has a
special construct for it, called "properties", which fills pretty much the
same purpose as the bean pattern in Java (the get* and set* prefixes).

In C# the accessors for your example would look like:

---------------------

class Mirror{

private int _width;
private int _height;

public Mirror(width, height)
{
_width = width;
_height = height;
}

public int Width
{
get { return _width; }
}

public int Height
{
get { return _height; }
}
}

---------------------

As your first example didn't have any mutators, I excluded them here as
well, but to give an example of that as well:
public int Width
{
get { return _width; }
set { _width = value; }
}
/// Bjorn A

Nov 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.