473,506 Members | 16,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getter and setter methods in C#?

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
3 99376
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
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

"Martin Pöpping" 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
18061
by: Kiwi | last post by:
Hello. I know a getter can return other thing than a field. I know a setter can do more things than setting a field. I know there are "setter only" cases and "getter only" cases. I do use...
8
6572
by: Herve Bocuse | last post by:
Hi, I'm just wondering what are the guidelines for using a Property or a pair of get/set methods related to a member variable ? What do yu guys do ? Thank you Herve
5
2394
by: Greg Scharlemann | last post by:
For some reason, I am having trouble retrieving the data that I store in the object that I have created from a database query. I created this class Lead (see below). In the php page, I create and...
1
1912
by: Steve | last post by:
I generate C# webservices proxy code from WSDL file, it turns out the classes generated have public member variables and no getter/setter methods as follows, and I am able to get data when...
5
6220
by: kronrn | last post by:
Hi Folks Can anyone confirm that the code public string Name { get; set; }
26
2500
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
5
2501
by: javatech007 | last post by:
Hi, I've been at this question all day and still just don't know what to do. It's the last of all the questions I have to do and can't figure it out!! If anyone could help or give guidance it...
3
5386
by: nelsonbrodyk | last post by:
Hey all, Just curious, I want to make the "set" in a property obsolete, but not the get or the property. Is there a way to do this? Public string FirstName { get; set; } works fine, but...
9
3220
by: jeddiki | last post by:
Hi, I have just started learning OOP and have a couple of questions. Is it correct to say that if I write a constructor method for my class then I will not need to use he set method to add...
0
7103
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
7307
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,...
1
7021
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
7478
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
5614
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,...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.