Tigraine

Daniel Hoelbling-Inzko talks about programming

C# Talk

Now and then I delve through some Java tutorials or Java applications for my studies. And everytime I do so I immediately lack the getter/setter language features of C#.

To make myself clear (to non-C# Developers). Java has either Public or Private properties, but no way to add validation Logic etc to those. It is usually considered bad coding technique to expose instance variables outside of the class so almost everything has to be accessed through functions. So to expose a integer variable called State you would have to create something similar to this:

get_set_Java 

It works and once you're used to it you will create setter/getter methods in your sleep. But, when pulling up your IntelliSense (ok, you don't have that in Java, but you know what I mean) on that class you will be somewhat overwhelmed by get/set functions :).

So, how did C# handle this? They borrowed a bit from Delphi and C# and created properties with get/set blocks (nothing new). So our code from above would look like:

get_set_C

The great thing: Intellisense gets easier, you have something clearly marked as property not as function (that's used to access a property). Usability for your API has just increased :).

But, despite the awesomeness of this function (already in the .NET Framework since 1.0) in .NET 3.5 they managed to improve this feature a bit. How? Imagine all those cases where you don't have any validator logic on your properties, you just want a simple public field in your class. In older Frameworks you'd have to create get/set Blocks for this Field although you don't want any logic there (exposing Instance-Variables is stupid no matter how you do it).

So, 3.5 Introduced this:

get_set_35

Great, we came down to 1 line of code, and what happened? .NET creates the private instance-variable automatically and handles the simple get/set stuff for us. When we decide we need validation or something in the get/set blocks we can just come back and expand those blocks without having to change any code outside the class (it was a get/set property all the time)

My Photography business

Projects

dynamic css for .NET

Archives

more