473,791 Members | 2,901 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 99393
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.myOtherFie ld;
}
}
}
-----------------------------------

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
18091
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 getters and setters when needed. But most of getter/setter pairs I have seen are just like below; > private int x; > public int getX() { return x; } > public void setX(int x) { this.x = x; }
8
6587
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
2453
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 array of Lead objects and later on down the page I iterate through the array of leads, retrieving each Lead and calling the get methods. However the get methods are not returning any values. I think I've copied the important parts of the code...
1
1925
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 running the client. public class MyFeeResponse {
5
6245
by: kronrn | last post by:
Hi Folks Can anyone confirm that the code public string Name { get; set; }
26
2548
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
2516
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 would be greatly appreciated!! Question: Write a class called shapes that accepts two variables into its constructor called a and b. Along with the getter and setter methods required, write a method called toPrinter() to output the coordinates of...
3
5405
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 this fails:
9
3234
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 values to properties ? i.e. with this:
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
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 we have to send another system

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.