I really don't like .NET or C#, I admit that pointers would be a great advantage but I can live without :p When you say myChild.age =- 10;, you can do that in Java by simply making a property public. I doubt you missed something like that out as you're a Java veteran so I'm guessing you're referring to Java's good coding practices when you mention that? As for the exception handling, I don't really see an issue there; can easily wrap in try/catch blocks.
Providing that the code behind myChild.setAge(-10); is only just:
public void setAge(int age){
this.age = age;
}
Overall, C# isn't as portable for my liking. I've never looked into it much and when I did I jumped right into XNA. Honestly my main put-off is the fact that Microsoft is directly behind it, so there's going to be some unfair competition control going on (like Apple is with Objective-C). MonoDevelop makes it look promising to me though. But maybe another time for me.
Actually, properties are not exactly variables. It's a way to control the integrity of data whilst avoiding to make explicit getters and/or setters.
Look at this piece of code in Java:
public class MyClass {
public int myAge = 10;
}
With that, you can simply write:
instance.myAge = 18;
But in that case, you could also set a negative age, which is an unacceptable value. Setting attributes public is a direct violation to encapsulation. Hence you need to control that:
public class MyClass {
private int myAge = 10;
public int getMyAge()
{
return myAge;
}
public void setMyAge(int myAge)
{
if(myAge < 0)
throw new IllegalArgumentException(
"Your age must be positive");
this.myAge = myAge;
}
}
Now everything is fine, you cannot do this anymore:
instance.myAge = 18;
because the attribute is not visible, the access modifier being private. You must thus use:
instance.setMyAge(18);
instance.setMyAge(-23);
And everything is working fine.
All good. But it's ugly. Making get's and set's, plus is's if you have boolean attributes, ex: isFinished().
C# found the right way to do it, in my opinion.
Let's start back with the class we had at first, let's assume it's in C#:
public class MyClass {
public int myAge = 10;
}
C# is same as Java, setting myAge to -10 will work, but shouldn't.
So we have two choices. Either go berserk with getters and setters and waste hours commenting every one of them, and then having to remember whether or not you have done it for every variable. Oooooor, you can use properties. Properties is declared as a variable, with a name convention as a class, and instead of being attributed a value, it is attributed a block of code, where you can specify the get and set conditions. The value reserved keyword takes the variable that is attributed in the set. Look at this marvelous piece of code:
public class MyClass {
private int myAge = 10;
public int MyAge{
get{
return myAge;
}
set{
if(value < 0)
throw new ArgumentException(
"The age must be positive.");
myAge = value;
}
}
}
Now, imagine you set up a variable that cannot be modified, just don't write a set. Or put the set private, it's also possible. The way to use it now is:
instance.MyAge = 18;
instance.MyAge = -10
And what's amazing too, is that you can use the gets and sets without even noticing it.
Look at that:
//My birthday!
myself.MyAge++;
And it totally works!
What is the Java code for that again?
//My birthday!
myself.setMyAge(myself.getMyAge+1));
Beauuuutiful... -_-
So no more setMyAge and getMyAge, you just use MyAge instead of myAge and all validation is done for you. You can even create properties for non-existing variables, just as you could have a getter in Java that does some calculation. I'm using properties everywhere, it would be so easy for Java to implement such feature.
Oh also in C#, you can specify the operators, so doing instance1 + instance 2 could actually result in something. Like in Java we have such functions:
if(myDate.compareTo(myOtherDate)>0)
myNewDate = myDate.addDate(myOtherDate);
In C# you can specify what ==, >, <, >=, <=. != does and what +, -, *, /, ^ may do as well. So the above statement could be translated as:
if(myDate > myOtherDate)
myNewDate = myDate + myOtherDate;
Though I'm still using Java more than C#, I still find C# has made everything more easy and beautiful. Also, even though they're only for Windows, I quite like having .exe files because some of my friends don't like when their computer asks for the permission to use Java to view my stuff. While an exe opens right away.
Oh, and Visual Studio is a marvel