Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Benefits using Properties?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Yin99 - 08 Sep 2007 19:47 GMT
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
Peter Duniho - 08 Sep 2007 19:55 GMT
> 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
Scott M. - 08 Sep 2007 21:17 GMT
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
[quoted text clipped - 4 lines]
>
> Yin
rossum - 08 Sep 2007 23:05 GMT
>I programmed JAVA for a while and starting C#, and was experimenting
>with
[quoted text clipped - 4 lines]
>
>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
David Anton - 09 Sep 2007 02:40 GMT
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.
Signature

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

> I programmed JAVA for a while and starting C#, and was experimenting
> with
[quoted text clipped - 4 lines]
>
> Yin
Bill Butler - 09 Sep 2007 14:55 GMT
>I programmed JAVA for a while and starting C#, and was experimenting
> with
[quoted text clipped - 4 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.