C# from a Java Developer’s Perspective: Properties

Why Should I Care, Gavin?

What a great question, reader! In my months here at Microsoft, I’ve been spending time learning one of our programming languages of choice C#. Now, coming in as a college hire usually (not always) means that you’ve written a lot of code in Java. I’m here to tell you all that C# isn’t a far cry from Java in terms of things like syntax, but there are some key differences. In my opinion, I think these differences typically help the developer write code quickly and efficiently.

The Chart

Listed here are some key differences between C# and Java 7. Why Java 7 when 8 has been released, you ask? Well, it’s largely because one of the biggest platform people write Java code in is Android, and Android doesn’t really support Java 8 (yet?). But worry not! I’ve added an addendum for features present in Java 8.

C# vs Java

 

Properties

In C#, properties are members of fields in a class that allow us an easy way to read, write, or compute values. In Java, we’re pretty used to writing methods that handle getting and setting data (Bean Properties). The primary difference is that properties are built in C# to be easy to implement and easy to read! All one has to do is declare the properties in the class member of choice and use the same syntax to call the property as one would to make a method call. A sample class with C# properties implemented is below:

Property Example in C#

And there you have it! In this class, both Hours and Minutes are members with “get” and “set” properties. The “get”and “set” properties are simply used to convert back and forth between seconds, hours, and minutes.

That’s pretty cool, but the real syntactic sugar comes in when we only need to use trivial “get/set” properties. an example is below.

Now, how quick and easy does that look? Calling these properties is pretty easy and self explanatory as well.

And there you have it! Calling a property of a class’s data member is as easy as calling any ol’ method.

Now, as awesome as this is, properties alone aren’t enough to get anyone all hot and bothered about C#. Trust me, there’s some cool stuff coming up!

 

 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *