Tuesday, 7 July 2009

C# envy: properties

Recently I found out that in c#, if you want to put a variable behind a public getter and private setter, so that it can be read but not changed, you can do it on one line! Here it is:

public int x { get; private set; }
Or if you want both public, it would be:

public int x { get; set; }

In ActionScript3 this would be:
private var _x:int;

public function get x():int
{
return _x;
}

public function set x(_x:int):void
{
this._x = _x;
}
The c# way is so much nicer - feature request Adobe!

7 comments:

zproxy said...

In actionscript3 the set and get must share the same namespace/modifier thus you cannot have private setter and public getter.

Did you know you could convert c# to actionscript? :P

Iain said...

@zproxy

Yeah but you can have no setter at all, which pretty much achieves the same thing - a read-only property.

"Did you know you could convert c# to actionscript? :P"

- what automagically? how? (what happens to enums etc?)

Keith Peters said...

Then there is Objective-C:

@property int x;
@synthesize x;

Ickydime said...

zproxy is talking about Alchemy:

http://labs.adobe.com/wiki/index.php/Alchemy

Iain said...

@Ickydime Alchemy is for c/c++ right? not c# - way different animal

Iain said...

@Keith Peters - thanks for the input by the way - always fun to peer over the fence at other languages.

dave.dolan said...

There's a lot of other features C# has that I miss in action script: generics, the ability to include libraries from any other CLR platform languages, the ability to query remote domains, etc. Flash has one gigantic thing going for it though: the ability to run in browser on most platforms with hardware acceleration. So far, and Silverlight is playing catch up, big time.