Cross-Training in Silverlight & Flex — C#/CLR vs ActionScript3/AVM Part 1

More Cross-Training in Silverlight & Flex

Shout it

In my previous post, I talked about the declarative side of Silverlight and Flex (XAML vs MXML).  In this post, I will talk about the imperative side Silverlight and Flex.  In other words, this is where developers write code that executes logic to modify state.  The primary languages for Silverlight and Flex are C# and ActionScript (there are others) and the execution run-times are the Common Language Runtime (CLR) and the ActionScript Virtual Machine (AVM).

C#/CLR

The most widely used language in Silverlight is C#, a C-like language that is easy to use/read and it is very powerful.  It is an object-oriented language, though it borrows concepts from functional languages.  C# code compiles down to byte code which gets executed on the Common Language Runtime (CLR).  As its name implies, the CLR can run languages other than C#.  The supported language list is quite long, but some of the more common options are VisualBasic.Net, F#, IronRuby and IronPython.  Since C# is the most commonly used, I will focus on it in this post.

Hello World

To start things off, here is a “Hello World” application written in C#.  It is quite similar to ActionScript because they are both object-oriented languages.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
    }
}

 

Static by Default, Dynamic by Option

Since its inception, C# has been a static language.  Microsoft introduced optional dynamic typing in C# 4.0 with the dynamic keyword.  In other words, variables defined as dynamic use late binding to resolve properties and methods where all other variables are checked at compile-time.

// STATIC EXAMPLE
Turkey turkey = new Turkey();
Console.WriteLine(turkey.Weight);
var steamShovel = new SteamShovel(); // Implicit Typing
Console.WriteLine(steamShovel.Weight);
Console.WriteLine(steamShovel.NotThere); // Compile-time Error

// DYNAMIC EXAMPLE
dynamic item = new Turkey();
Console.WriteLine(item.Weight);
item = new SteamShovel();
Console.WriteLine(item.Weight);
Console.WriteLine(item.NotThere); // Runtime Error

 

Classes/Interfaces

C# defines interfaces, classes and derived classes very similar to ActionScript or Java:

public interface IGobbleable
{
    void Gobble();
}

public class Turkey : IGobbleable
{
    public int Weight { get { return 5; } }

    public virtual void Gobble()
    {
        Console.WriteLine("Gobble Gobble");
    }
}

public class WildTurkey : Turkey
{
    public override void Gobble()
    {
        Console.WriteLine("Putts Putts");
    }
}

In “Part 2” of my imperative programming comparison, I will cover Generics, Iterators, Attributes, Lambdas and XML Literals.

 

ActionScript3/AVM

The most widely used language in Flex is ActionScript3, an ECMAScript dialect that is easy to use/read and is quite powerful (though not as powerful as C#, in my opinion).  Although it is often compared to another widely used ECMAScript language, JavaScript, it diverges in a way that feels more like Java.  Like Java, ActionScript3 is object-oriented with classes and interfaces.  It also has optional static typing which JavaScript does not have.  ActionScript3 code compiles down to ActionScript Byte Code (ABC) and runs on the ActionScript Virtual Machine (AVM2) – also known as Tamarin

Although ActionScript is the only official language supported on the AVM2, there are several options for developing in Flex.  Adobe Labs released a preview of Alchemy, a tool for compiling C/C++ code to run on the AVM2.  The widely-known haXe language, which is like ActionScript on steroids, also compiles for the AVM2.  In addition, people have gotten Ruby (via HotRuby), C# (via cs2asJ) and Java (via J2AS3) to run in the Flash player – therefore lending to Flex development.

Since ActionScript3 is the most commonly used language in Flex, I will focus on it in this post.

Hello World

To start things off, here is a “Hello World” application written in ActionScript3.  It is quite similar to C# because they are both object-oriented languages.

public class HelloWorld extends Sprite
{
    public function HelloWorld()
    {
        trace("Hello World");
    }
}

 

Dynamic by Default, Static by Option

Since its inception, ActionScript has been a dynamic language.  Adobe introduced optional static typing in ActionScript 2 with a colon syntax.  In other words, variables defined as static will remain static and will be checked at compile-time where variables without type information will be dynamic and late-bound (run-time).

// DYNAMIC EXAMPLE
var item = new Turkey();
trace(item.weight);
item = new SteamShovel();
trace(item.weight);
trace(item.NotThere); // Runtime Error

// STATIC EXAMPLE
var turkey:Turkey = new Turkey();
trace(turkey.weight);
var steamShovel:SteamShovel = new SteamShovel();
trace(steamShovel.weight);
trace(steamShovel.NotThere); // Compile-time Error

 

Classes/Interfaces

ActionScript defines interfaces, classes and derived classes similar to C# with a syntax that is more like Java:

public interface IGobbleable
{
    function gobble():void;
}

public class Turkey implements IGobbleable
{
    public var weight:int = 5;

    public function gobble():void
    {
        trace("Gobble Gobble");
    }
}

public class WildTurkey extends Turkey
{
    public override function gobble():void
    {
        trace("Putts Putts");
    }
}

In “Part 2” of my imperative programming comparison, I will cover Generics (via Vector.<>), Proxy objects, MetaData Tags, Anonymous Functions and XML Literals.

Stay tuned Smile

Tags: , , , , ,

3 Responses to “Cross-Training in Silverlight & Flex — C#/CLR vs ActionScript3/AVM Part 1”

  1. Cross-Training in Silverlight & Flex — C#/CLR vs ActionScript3/AVM Part 1…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  1. Dan East says:

    Hi Brian,
    I’ve also been tasked with comparing Silverlight with Adobe Flex/Flash/AIR, so your blog posts are timely and very informative. I look forward to future posts. Interesting how some companies with WPF / Silverlight expertise are now using Flex/Flash in their browser apps (AutoCAD-WS) and AIR for apps across multiple platforms (NY Times Reader). Microsoft even went the HTML/CSS/JavaScript route for their Office Web Applications and only used Silverlight sparingly.
    Dan

Leave a Reply