473,248 Members | 1,857 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,248 software developers and data experts.

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 99354
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 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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
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
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
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
by: kronrn | last post by:
Hi Folks Can anyone confirm that the code public string Name { get; set; }
26
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.