Benefits using Properties?

  • Thread starter Thread starter Yin99
  • Start date Start date
Y

Yin99

I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin
 
Yin99 said:
I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

I know very little about Java, but my understanding is that Java does in
fact have a de facto convention for supporting properties. They are
explicitly implemented using "get" and "set" as part of the method
names, but I have the impression that when you follow the correct
convention, they have most of the benefits of C# properties.

If I'm wrong about that, no doubt someone who actually uses Java and
understands it will provide a correction. :)

As far as why properties are useful, I think you would be better served
by reading threads already covering the topic in this newsgroup (find
them using Google Groups) and of course reading the MSDN documentation
regarding how properties can be used.

That said, the short answer is that properties provide a way of
encapsulating values maintained by a class, as well as providing a
standardized way to represent that encapsulation so that the values can
be used in other aspects (for example, using properties in data binding,
using them for automatic serialization of a class, etc.)

You can accomplish the former simply by using explicit getter and setter
methods, but IMHO the code is more readable using properties, and of
course doing it that way prevents any of the benefits related to the
latter issue, since the getter and setter methods wouldn't be reliably
identifiable as different from any other method.

Pete
 
The Java concept of setting and getting class instance and virtual method
values is what is more formally known as "properties". So, yes, you do have
properties in Java. This makes your main question mute. If you can
understand why a class should hold data that is related to the class
instance, then you understand why properties are an integral part of most
classes. Technically, properties aren't a C#, Java, or any language for
that matter, thing. They are a a basic tenant of Obejct Oriented
Programming.
 
I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin
C# properties are syntactic sugar - nice to have but not essential.

C#:

widget.Field = 45;
myVariable = widget.Field;

Java

widget.setField(42);
myVariable = widget.getField();

rossum
 
It is just syntactic sugar, but then again syntactic sugar is a big part of
any programming language.
I would phrase the question the opposite way: given that most other modern
programming languages (C++/CLI, VB, C#, Python, ...) have properties, why
doesn't Java? Not to mention other basic elements of modern languages:
delegates, events, operator overloading, a preprocessor, and 'ref' parameters
- some of this stuff sure comes in handy.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
 
Yin99 said:
I programmed JAVA for a while and starting C#, and was experimenting
with
"Properties". Question I have, is what is the benefit? JAVA does
not have the concept
of properties, so what is JAVA missing and why is it so great C# has
it? Thanks,

Yin

As other have pointed out, properties are the C# equivalent of
setter/setter methods.
You could write C# code using the same getter/setter syntax as in Java
if you prefer, but properties improve the readability of the code. Below
are similar classes in Java and in C#.

------------------------------------
//// Java ////
public class Foo
{
private int x = 0;

public int getX() {return x;}
public void setX(int value) {x = value;}
}

//// C# ////

public class Foo
{
private int x = 0;

public int X
{
get {return x;}
set {x = value;}
}
}

-----------------[ setter example ] ----------------
// Java - You call the setter method explicitly
Foo foo = new Foo();
foo.setX(5);

// C# - X looks like a public field,
// but it really calls the setter under the covers
Foo foo = new Foo();
foo.X = 5;

-----------------[ getter example ] ----------------
// Java - You call the getter method explicitly
int x = foo.getX();

// C# - X looks like a public field,
// but it really calls the getter under the covers
int x = foo.X;

-----------------[ += example ] ----------------
// Java : += logic is a bit painful to look at

// add 2 to foo.x
foo.setX(foo.getX + 2));

// C# - Under the covers, this is doing the same thing as the java code
foo.X+=2;

-----------------
If you are interested in the underlying functionality try running this
code.
It explicitly shows how the setter/getter are called

using System;

public class Test
{
public static void Main()
{
Foo foo = new Foo();
foo.X = 5;
Console.WriteLine("Foo.X:{0}", foo.X);

foo.X += 2;
Console.WriteLine("Foo.X:{0}", foo.X);
}
}

public class Foo
{
private int x = 0;

public int X
{
get
{
Console.WriteLine("Foo.getX:{0}", x);
return x;
}
set
{
x = value;
Console.WriteLine("Foo.setX({0})", x);
}
}
}

Hope this helps
Bill
 

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

Back
Top