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() );
}