Dynamic. NET

Source: Internet
Author: User
Tags closure silverlight

One of the key features of Silverlight is support for dynamic languages such as IronRuby and IronPython. With this integration, you can use the Silverlight platform to develop rich Internet applications (RIA)-XAML for presentation tiers, and dynamic languages for code-behind. This article demonstrates the integration capabilities of Silverlight with dynamic languages and the Microsoft Bing Map control. First, I'll briefly introduce the dynamic language and then delve into the support that Silverlight has for these languages. Finally, as a summary, I will demonstrate how to use the Microsoft Bing Map Silverlight Control and IronRuby to generate interactive 3D animation locations to find Silverlight applications.

Basic knowledge of dynamic language

The Read-eval-print Loop (REPL) environment provides lightweight "write-while" programming functionality for developers by using dynamic programming languages, which are dynamically typed and compiled at run time. You do not need to declare a variable of a particular data type. All code is handled by the runtime through an expression context.

Languages that are more familiar to us, such as C # and Visual Basic, are statically typed languages and are lesser in flexibility. Using dynamic languages for development and deployment is simpler than static languages that need to compile and distribute output. However, when using dynamic type languages, you still need to validate and test type security.

When you use a dynamic language, you can create a function and then assign it to a variable or pass it to another function as a parameter. This makes it easier to use closures and pass functions as parameters. Typically, closures have two distinct features: You can assign code blocks (functions) to variables, and code blocks can retain access to variables that are accessible at creation time.

The following is a traditional Shortwords method written in C # that returns a subset of Word lists that contain words that meet the condition that the word is not longer than 3:

public static List<string> ShortWords(List<string> wordList) {
  List<string> shortWordList = new List<string>();
  int maximumWordLength = 3;
  foreach(string word in wordList) {
   if(word.Length <= maximumWordLength) {
    shortWordList.Add(word);
   }
  }
  return(shortWordList);
}

Using LINQ, you can implement similar functionality more efficiently, as shown in the following code snippet:

public static List<string> ShortWords(List<string> wordList) {
  var maximumWordLength = 3;
  return wordList.Where(w => w.Length <=
    maximumWordLength).ToList<string>();
end

Implementing this method in dynamic languages such as IronRuby (the Ruby programming language implementation of the Microsoft. NET Framework) is similar to using LINQ methods in C #, but is much shorter than traditional methods:

def ShortWords(wordList)
  maximumWordLength = 3
  return wordList.select {|w| w.Length <= maximumWordLength}
end

By comparing these two implementations of these algorithms, you can learn a lot about IronRuby (and other dynamic languages). The IronRuby code is very concise and does not contain any data type keywords (such as string or int).

The most interesting of this IronRuby code block is the closure between curly braces. The closure here is actually a function that is passed to the Select method. The Select method uses a closure to extract a subset of the collection. The code that makes up the closure actually executes within the Select method (where the closure extracts a qualifying string in the collection Wordlist), but retains access to the variable in its original scope (in this example, the Maximumwordlength variable).

Closures are very powerful, and this simple example is for demonstration purposes only. Closures are similar to using LINQ in C # or passing delegates to Exists or find methods, and closures have the advantage of retaining access to their original scope. For more information on closing a package, see the book "Accelerated Silverlight 3" (apress,2009 July), which I co-authored with Jeff Scanlon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.