Sunday, May 29, 2005

C# Feels Cleaner Than Java:


I have to agree with Daniel with regards Java 3. Having performed a little experiment; define a class with a property and an event notification mechanism in both Java and C#. C# does appear to be a clearer language, mainly due to the event keyword, and the property syntax.

public class SimpleBean implements Serializable {
private String name;
private PropertyChangeSupport pcs;

public SimpleBean() {
super();
pcs=new PropertyChangeSupport(this);
}
public String getName() {
return name;
}
public void setName(String name) {
String oldName = this.name;
this.name = name;
pcs.firePropertyChange('Name',oldName,this.name);
}
public void addPropertyChangeListener(PropertyChangeListener pcl) {
pcs.addPropertyChangeListener(pcl);
}
public void removePropertyChangeListener(PropertyChangeListener pcl) {
pcs.removePropertyChangeListener(pcl);
}
}

[Serializable]
public class SimpleBean
{
private string n=String.Empty;

public event EventHandler Change;

public SimpleBean() {}

public string Name
{
get {return n;}
set {
n=value;
EventHandler changes = Change;
if (changes != null)
{
changes(this, new EventArgs());
}
}
}
}
Note: The example code is not directly comparable with regards to the event data

0 Comments:

Post a Comment

<< Home