getter and setter methods in C#?

G

Guest

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
 
J

Jeroen

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;
}
}
}
 
M

Marc Gravell

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
 
B

Bjorn Abelli

Martin Pöpping said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top