Dynamic typing and dynamic programming in C # 4.0

Source: Internet
Author: User

With the development of the web, a variety of dynamic language also through the Dongfeng, booming. In the software development world, dynamic language is being accepted and used by more and more people, and in the 2007 readership survey conducted by CSDN, dynamic language has reached 12% in the development crowd. In Tiobe's rankings, the dynamic language occupies six of the top 10 (including PHP, Python, Perl, and JavaScript, plus increasingly dynamic Java and C #).

"The future belongs to the dynamic language" seems to be becoming a reality from a prophecy. C # Naturally does not miss this technological development indeed, she is becoming more and more beautiful "moving" people by constantly introducing the characteristics of new dynamic prophecies.

The main theme of C # 4.0 is Dynamic programming (dynamical programming). Although C # is still a static prophecy, the meaning of the object begins to become more and more "dynamic". Their structure and behavior cannot be captured by static types, or at least the compiler does not know the structure and behavior of the object when compiling the program.

C # introduces a new static type "dynamic", and when you have an object of the dynamic type, what you "do with it" is parsed only at run time. Imagine that we have two classes that represent two kinds of beverages, respectively:

public class Coffee
   {
     public string GetName()
     {
       return "You selected Maxwell coffee.";
     }
   }
   public class Juice
   {
     public string GetName()
     {
       return "You selected orange juice.";
     }
   }

Now, we can use the dynamic type to represent the two drinks. We write a function getdrink (), more user different choices to return different objects.

static private Object GetDrink(int i)
{
       if (i == 1)
       {
         return new Juice();
       }
       else // default
       {
         return new Coffee();
       }
}
static void Main(string[] args)
{
       Console.WriteLine("Please Select Your Drink: 1 -- Juice; 2 -- Coffee");
       int nDrinkType = Console.Read();
       dynamic drink = GetDrink( nDrinkType );
       Console.WriteLine( drink.GetName() );
}

Related Article

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.